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 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错