CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
(图)邪恶的韩国UMPC 使用 Java 编写数据库应用新规范
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C++ 语言

文件流问题

楼主hybest()2006-12-01 17:54:04 在 C/C++ / C++ 语言 提问

使用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

相关问题

关键词

得分解答快速导航

  • 帖主:hybest

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
惹火投票。。火热进行中...
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo