初学C++的文件操作,请指点一下
下面的代码只是实现了对 record.txt 文件的追加输出。
但我想对 record.txt 中的某条记录进行修改,请指点一下怎么写
例如:Jack 从北京搬家到杭州,则如何修改其中的记录呢??
number name address
------------------------
1001 Jack Beijing
改成:
------------------------
1001 Jack Hangzhou
#include <iostream>#include <fstream>#include <cstdlib> using namespace std;int main(){ // ofstream 构造函数打开文件 ofstream outClientFile( "record.txt", ios::app ); //以追加的方式输出 if ( !outClientFile ) //测试文件是否被正确打开 { cerr << "File could not be opened" << endl; exit( 1 ); } cout << "Enter the number, name, and address.\n" << "Enter end-of-file to end input.\n? "; int number; char name[ 30 ]; char address[ 30 ]; while ( cin >> number >> name >> address ) { outClientFile << number << ' ' << name << ' ' << address << '\n'; cout << "? "; } return 0; // ofstream 析构函数关闭文件}#include <fstream>using namespace std;int main(){ const char* filename = "test.txt"; //just build a little sample file ofstream sampleFile(filename); sampleFile << "This is a sample content "; sampleFile.close(); //declare a new file object for appending and "overwriting"... fstream file(filename, ios::ate | ios::out | ios::in); //...mark the current position... long changePos = file.tellp(); file << "This text will be changed "; //..append something else file << "Append some other text"; //...overwrite previous contents file.seekp(changePos); file << "changed text "; file.close(); return 0;}
[解决办法]
以下为简易版本,有时间我在加强下。没有出错处理,仅能处理一行!小心使用,仅供参考!
#include <iostream>#include <fstream>#include <sstream>//这是我添加的,用于istringstream#include <cstdlib> #include <string> using namespace std;int main(){ // ofstream 构造函数打开文件 char* filename="Record.txt"; fstream file(filename, ios::trunc | ios::out | ios::in); //以空白的方式输出 if ( !file ) //测试文件是否被正确打开 { cerr << "File could not be opened" << endl; exit( 1 ); } cout << "Enter the number, name, and address.\n" << "Enter end-of-file to end input.\n? "; int number; string name; string address; while ( cin >> number >> name >> address ) { file << number << ' ' << name << ' ' << address << '\n'; cout << "? "; } // string one_line,word; string target="Beijing"; file.seekp(0,fstream::beg); while(getline(file,one_line)) { //fstream::pos_type mark=file.tellg(); istringstream stream(one_line); stream>>number >> name >> address; //cout<<address<<endl; string::size_type pos1=one_line.find(address); //cout<<pos1<<endl; one_line.replace(pos1,one_line.size()-1,target); file.seekp(0,fstream::beg); file<<one_line<<'\n'; } return 0; // ofstream 析构函数关闭文件}
[解决办法]
//文本覆盖、插入、修改,简单的示例一下:#include <stdio.h>#include <stdlib.h>int main(){ FILE *fp; char *insert = "EE,EE,EE,", tmp[256]={0}; int pos, i; /*test.txt文件内容:01,00,00,00,0E,00,00,00,00,00,00,E1,E2,00,00,00,00*/ fp = fopen("test.txt", "r+"); /*1、等长覆盖*/ fseek(fp, 12, 1); fprintf(fp, "0F"); /*直接写入等长度的数据就可以完成覆盖*/ /*2、E2后插入“EE,EE,EE,” */ fseek(fp, 25, 1); pos = ftell(fp); /*插入位置*/ fgets(tmp, 256, fp); /*把后面的数据缓存*/ fseek(fp, pos, 0); /*移动到插入位置*/ fprintf(fp, insert); /*插入,就是写入数据*/ fprintf(fp, tmp); /*把原来的数据再写回来, 完成*/ /*3、01读取后++两次,并重新写回文件*/ rewind(fp); fscanf(fp, "%x", &i); /*读取数据*/ i = i+2; /* +2 */ fseek(fp, -2, 1); /*调整指针*/ fprintf(fp, "%02x", i); /*写文件*/ fclose(fp); system("PAUSE"); return 0;}