文件输出问题
使用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()
{
ifstream in;
in.open ("test1.txt",ios_base::out);
char ch[20];
in>>ch;
cout<<ch<<endl;
return 0;
}
编译没问题,但没输出结果,为什么?
问题点数:10、回复次数:4Top
1 楼taodm((不能收CSDN社区短信息,请莫浪费精力))回复于 2006-12-04 09:07:38 得分 0
in.open ("test1.txt",ios_base::out);
用in模式打开文件。直接in.open ("test1.txt");即可。Top
2 楼tracing(攀登者)回复于 2006-12-04 09:46:31 得分 0
我试了上面的代码,没问题,有结果Top
3 楼OOPhaisky(异化$渴望成功~~)回复于 2006-12-04 12:13:27 得分 0
upTop
4 楼lann64(昆仑大鹏@迦楼罗)回复于 2006-12-04 12:19:33 得分 0
in.open ("test1.txt",ios_base::out);
以输出方式打开输入文件?Top




