请教Compiler Explorer 中读写文件问题?

weixin_39015430 2024-12-10 11:43:39

本人正在用Compiler Explorer 学习C++,现遇到一个问题是:用程序打开本地计算机的文件时,无法读出data.txt中的内容,编译后没有任何输出。请教各有识之士,data.txt是否需要上传到Compiler Explorer 哪里保存,或者如果要读写本地的文件,in中的文件路径该怎么写?程序如下:

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

using namespace std;

 int main()

 {

    ifstream in("d:\\data.txt");     //in中的文件路径该怎么写?

    if(!in)

    {

        cerr << "无法打开输入文件" << endl;

        return -1;

    }

 

    string line;

    vector<string> words;

    while(getline(in,line))

    {

        words.push_back(line);

    }

 

    in.close();

 

    vector<string>::const_iterator it = words.begin();

    while(it != words.end())

    {

        cout << *it << endl;

        ++it;

    }

    return 0;

 }

...全文
337 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2024-12-11
  • 打赏
  • 举报
回复

可能是文件编码不对,用记事本打开d:\data.txt,另存为,设置ANSI编码

65,183

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧