c++读写

来源:互联网 发布:java防止xss攻击代码 编辑:程序博客网 时间:2024/06/05 18:32

这是大神们的总结



ios::in为输入(读)而打开文件ios::out为输出(写)而打开文件ios::ate初始位置:文件尾ios::app所有输出附加在文件末尾ios::trunc如果文件已存在则先删除该文件ios::binary二进制方式

fstream类

#include <iostream.h>#include <fstream>using namespace std;int main(){<span style="white-space:pre"></span>ifstream in("myfile.txt");<span style="white-space:pre"></span>ofstream out("myfile1.txt");<span style="white-space:pre"></span>if(!in||!out)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>return -1;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>char buf[1000];<span style="white-space:pre"></span>while(!in.eof())<span style="white-space:pre"></span>{<span style="white-space:pre"></span>in.getline(buf,sizeof(buf));<span style="white-space:pre"></span>out<<buf;<span style="white-space:pre"></span>in.read(buf,sizeof(buf));<span style="white-space:pre"></span>out.write(buf,in.gcount());<span style="white-space:pre"></span>}<span style="white-space:pre"></span>in.close();<span style="white-space:pre"></span>out.close();<span style="white-space:pre"></span>return 0;}


0 0
原创粉丝点击