Peniisia Puzzy 2022-03-21 22:22 采纳率: 100%
浏览 20
已结题

请问一下有关c++读取txt的问题

我想把txt文件里面的单词按照行和列放入一个char的二维数组里面 (默认一行最多1000个单词) 但是写出来后发现乱码了 请问这个读取过程有什么问题吗?

img

txt文件是这个:
I gazed—and gazed—but little thought
What wealth the show to me had brought:
For oft, when on my couch i lie
In vacant or in pensive mood,
They flash upon that inward eye
Which is the bliss of solitude;
And then my heart with pleasure fills,
And dances with the daffodils.

  • 写回答

1条回答 默认 最新

  • _GX_ 2022-03-21 22:53
    关注

    你的代码有不少问题。既然是C++,就尽量用STL库。

    #include <iostream>
    #include <vector>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        ifstream infile("testfile.txt");
        vector<string> lines;
        string line;
        while (getline(infile, line))
            lines.push_back(line);
        for (const auto &line : lines)
            cout << line << '\n';
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月30日
  • 已采纳回答 3月22日
  • 修改了问题 3月21日
  • 创建了问题 3月21日

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题