请问,问题出在哪里?

iwantfat 2005-01-19 09:07:33
#include <iostream>
#include<fstream>
#include<string>
#include<cassert>
using namespace std;
void main()
{
ofstream outfile("file1.txt");
ifstream infile("file2.txt");
assert(outfile);
assert(infile);
string word1;
while(infile>>word1)
{
outfile<<word1;
//cout<<"\n";
}
//cout<<"have no word out"<<endl;
system("pause");
}
上面的代码是在dev—c++中编译的,其中file2.txt 总是创建不了,往file.txt写字符串失败。请问怎样修改才能解决这个问题。
...全文
103 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
TomDebug 2005-01-19
  • 打赏
  • 举报
回复
file2应该本来就有的,不用创建
易旭昕 2005-01-19
  • 打赏
  • 举报
回复
你可能理解错了,file2.txt是输入的文件,应该是事先存在的,而不是创建的。
程序的左右是把file2.txt里面的单词拷贝到新建的file1.txt里面去。
在当前目录下新建一个file2.txt文件,在里面随便敲几个单词进去,然后再运行程序试试。
xingji_h 2005-01-19
  • 打赏
  • 举报
回复
#include <iostream>
#include<fstream>
#include<string>
#include<cassert>
using namespace std;
void main()
{
ofstream outfile("file1.txt");
ifstream infile("file2.txt");
assert(outfile);
assert(infile);
string word1;
while(infile>>word1)
{
outfile<<word1;
//cout<<"\n";
}
//cout<<"have no word out"<<endl;
infile.close();
outfile.close();
system("pause");
}

这样就可以了,你没有关闭文件
liixixi 2005-01-19
  • 打赏
  • 举报
回复
ios
sandrowjw 2005-01-19
  • 打赏
  • 举报
回复
只读方式不会自动创建文件的,只有写方式会创建或者清空文件。
syilo 2005-01-19
  • 打赏
  • 举报
回复
UP!

64,654

社区成员

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

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