类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
把文件的第一行替换成自己的内容,请问该怎样操作?小弟对文件的基本操作不熟悉啊!等待....
网友回答:
#include <vector>
#include <string>
#include <fstream>
using namespace std;
string const my_line = "abcdefg";
string const filename = "c:\\test.txt";
int main()
{
string line;
vector<string> line_list;
{
ifstream infile(filename);
getline(infile, line);
while(getline(infile, line))
line_list.push_back(line);
}
{
ofstream outfile(filename);
outfile << my_line << "\n";
line_list
for(vector<string>::iterator it = line_list.begin(); it != line_list.end(); ++ it)
outfile << *it << "\n";
}
}
不要酱紫,这位小弟可能对stl还不太了解.....
#pragma warning(disable:4786)
int main(int argc, char* argv[])
{
string my_line="abcdefg";
string line;
vector<string> line_list;
ifstream infile("c:\\text.txt");
infile>>line;
while(infile>>line)
line_list.push_back(line);
ofstream outfile("c:\\text.txt");
outfile << my_line << "\n";
for(vector<string>::iterator it = line_list.begin(); it != line_list.end(); ++ it){
outfile << *it << "\n";
cout<<*it;
}
outfile.close();
return 0;
}
拷贝CopyFile作一个备份临时文件。 然后一行行读去这个文件内容,
ifstream in("备份文件");
in.getline(char *,int,\n);
第一次读的文字不写入文件,把他替换成你要的字符后写入文件。
ofstream ou("文件");
out<<"要替换的字符"<<endl;
其他的行,怎么读出来的就怎么写进去就可以了.
完成后 DeleteFile把备份文件删除就可以了
ofstream XXXX("xxx",ios::ate)打开文件
XXXX.seekp()设定指针。
XXXX.write()写入数据。
不用中间文件。