C++ Again(1):文件读入与写出
来源:互联网 发布:淘宝店铺产品全部下架 编辑:程序博客网 时间:2024/06/04 19:23
本文章的实现参考自<C++ Primer>第一章第5节。
当前的任务是实现一个C++程序,能够从某个文件读入字符串并将字符串写入到另一个文件中。
实现代码如下:
#include <iostream>#include <fstream>#include <string>using namespace std;int main(){ ofstream outfile("out_file"); ifstream infile("in_file"); if(! infile){ cerr<<"error:unable to open file"<<endl; return -1; } if(! outfile){ cerr<<"error:unable to open outfile"<<endl; return -2; } string word; while(infile >> word) outfile << word << '~'; return 0;}
in_file:this is a cat and that is a dog
out_file:this~is~a~cat~and~that~is~a~dog~
需要探究的问题:1)标点符号如何处理?2)如何实现写入out_file的时候将新的字符串写入到文件的最后面,而不是取代文件的内容。
3)ofstream ifstream新建对象的语句
1)以空格切分字符串,所以标点符号与普通字符没有区别
3)使用ofstream out_file = new ofstream("out_file")出错;
#include <iostream>#include <fstream>#include <string>using namespace std;int main(){ // ofstream outfile("out_file"); // ifstream infile("in_file");ofstream outfile;ifstream infile;outfile.open("out_file");infile.open("in_file"); if(! infile){ cerr<<"error:unable to open file"<<endl; return -1; } if(! outfile){ cerr<<"error:unable to open outfile"<<endl; return -2; } string word; while(infile >> word) outfile << word << '~'; return 0;}
2)参考自:http://blog.sina.com.cn/s/blog_66474b160100wgan.html
#include <iostream>#include <fstream>#include <string>using namespace std;int main(){ // ofstream outfile("out_file"); // ifstream infile("in_file");ofstream outfile;ifstream infile;outfile.open("out_file",ios::app);infile.open("in_file"); if(! infile){ cerr<<"error:unable to open file"<<endl; return -1; } if(! outfile){ cerr<<"error:unable to open outfile"<<endl; return -2; } string word; while(infile >> word) outfile << word << '~'; return 0;}
- C++ Again(1):文件读入与写出
- C++ 文件读入写出
- C和C++中的文件读入写出区别
- C和C++中的文件读入写出区别
- 关于c#文件流与二进制的读入写出(一)
- 关于c#文件流与二进制的读入写出(二)
- 关于C#文件流与二进制的读入写出(三)
- 北理工Java实验2.5(文件读入与写出)
- 图像的读入与写出
- C++与C的文件读入写入
- freopen读入/写出 数据 从/到 文件
- java文件流读入写出操作
- java 读入写出文件(终极版)
- 对象流的读入与写出
- java的输入输出流与文件操作(2.读入写出及关流)
- 文件的读入写出以及文件,文件夹的拷贝
- 【C/C++】C++读入文件
- C/C++读入写出空格和\0的区别
- Linux内核参数
- Genymotion配置及使用教程(最新最完整版附各部分下载地址)
- Facebook测试软件监控鼠标所有活动
- 数据库最主要特点可以用这几点来分类
- NSTimer与NSRunLoop的关系分析
- C++ Again(1):文件读入与写出
- AC自动机+DP小结 (一)
- Java版本问题
- GBase 8a 分析型数据库你该怎么去理解
- Oracle的directory详细路径查看、创建及修改
- 网络数据包发送接收全过程
- ZOJ 2320 Cracking' RSA
- Activity的Launch mode详解 singleTask正解 转
- Core Animation学习笔记