C++对文件的简单操作
/ *
程式的作用是:往文件(edit.txt)输入一行行的字符串,然后修改某一行的内容.要修改内容就必须用到目标文件(dest.txt).文件的内容一行行的读取,先确定要修改的内容是在哪一行,然后把此行之前的内容从文件(edit.txt)复制到文件(dest.txt),然后把此行的内容读入缓存,修改内容后再输出到(dest.txt),如果此行后面还有内容,就把剩余的直接从(edit.txt)复制到(dest.txt).
* /
程式的作用是:往文件(edit.txt)输入一行行的字符串,然后修改某一行的内容.要修改内容就必须用到目标文件(dest.txt).文件的内容一行行的读取,先确定要修改的内容是在哪一行,然后把此行之前的内容从文件(edit.txt)复制到文件(dest.txt),然后把此行的内容读入缓存,修改内容后再输出到(dest.txt),如果此行后面还有内容,就把剩余的直接从(edit.txt)复制到(dest.txt).
* /
#include <fstream>
#include <iostream>
#include <string>
#include <cctype>using namespace std;
#include <iostream>
#include <string>
#include <cctype>using namespace std;
int main(int argc, char* argv[])
{
char yn=0;
int count = 0,
total = 0,
j = 0;
const string NAME = “edit.txt”;
const string NAME2 = “dest.txt”;
string str;
string line;
ofstream onfile(NAME.c_str());
if (!onfile)
{
cout<<”cannot open the file…”<<endl;
}
onfile.clear(); //清空edit.txt的內容
fstream f2(NAME2.c_str());