文件流问题
使用I/O流以文本方式建议一个文件test1.txt,写入字符“已成功写入文件!”,用其他字符处理程序(例如Windows的记事本程序Notepad)打开,看看是否正确写入。
使用I/O流以文本方式打开上题建立的文件test1.txt,,在文件后面添加字符“已成功添加字符!”,然后读出整个文件的内容显示出来,看看是否正确。
我编的程序如下:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream out("test1.txt");
if(! out)
{
cout<<"cannot open file test1.txt"<<endl;
return -1;
}
char ch[20]={"已成功写入文件"};
out<<ch<<endl;
out.close();
}
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
fstream fout;
fout.open("test1.txt");
if(! fout)
{
cout<<"can not open test1.txt"<<endl;
return -1;
}
ofstream out("已成功添加字符",ios_base::app);
char ch[30];
fout>>ch;
cout<<ch<<endl;
fout.close();
return 0;
}
能不能告诉我哪出错了?
问题点数:40、回复次数:8Top
1 楼todototry(来csdn,学会扯淡了...)回复于 2006-12-01 18:11:43 得分 0
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream out("test1.txt");
if(! out)
{
cout<<"cannot open file test1.txt"<<endl;
return -1;
}
char ch[20]={"已成功写入文件"};
out<<ch<<endl;
out.close();
return 0;
}
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
fstream fout;
fout.open("test1.txt", ios_base::out);
if(! fout)
{
cout<<"can not open test1.txt"<<endl;
return -1;
}
//ofstream out("已成功添加字符",ios_base::app);
char ch[30] = "已成功添加字符";
fout<<ch;
cout<<ch<<endl;
fout.close();
return 0;
}Top
2 楼todototry(来csdn,学会扯淡了...)回复于 2006-12-01 18:12:05 得分 0
//ofstream out("已成功添加字符",ios_base::app);
char ch[30] = "已成功添加字符";
fout<<ch;Top
3 楼todototry(来csdn,学会扯淡了...)回复于 2006-12-01 18:12:19 得分 0
个人意见,高手多多指教Top
4 楼ddoq(ddoq)回复于 2006-12-01 19:04:34 得分 0
ofstream out("已成功添加字符",ios_base::app);
这也应该是ofstream out("filename",ios_base::app);Top
5 楼jixingzhong(瞌睡虫·星辰)回复于 2006-12-01 19:46:19 得分 0
ofstream out("已成功添加字符",ios_base::app);
呵呵, 第一个参数是你的文件名字啊 ...
比如 test.txtTop
6 楼hybest()回复于 2006-12-03 10:54:05 得分 0
要求是把'已成功添加字符'写入test1.txt,使yest1.txt最终显示"已成功写入文件!,已成功添加字符"!Top
7 楼OOPhaisky(异化$渴望成功~~)回复于 2006-12-03 12:39:48 得分 0
ofstream out("已成功添加字符",ios_base::app);
--------------------------------------------------------------------------------
"已成功添加字符",这里错了。Top
8 楼wind1985_1011()回复于 2006-12-03 14:28:12 得分 0
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
fstream fout;
fout.open("test1.txt",ios_base::out|ios_base::app);
char chr[20]={"已成功添加字符"};
char ch1[30],ch[30];
if(! fout)
{
cout<<"can not open test1.txt"<<endl;
return -1;
}
fout<<chr<<endl;
fout.close();
fout.open("test1.txt");
if(!fout)
{
cout<<"can not open test1.txt"<<endl;
return -1;
}
fout>>ch1;
fout>>ch;
cout<<ch1<<endl;
cout<<ch<<endl;
fout.close();
return 0;
}
Top





