这是在《C++高级编程(Professional C++)》上看到的例子。BTW,这真是一本很好的书,适合高阶的C++开发者阅读学习! void changeNumberForID( const string& inFileName, int inID, const string& inNewNumber) { fstream ioData( inFileName.c_str() ); if( !ioData ) { cerr << "Error while opening file "<< inFileName <<endl; exit(1); } //loop until the end of file while( ioData.good() ) { int id ; string number ; //read the next id ioData >> id; //check to see the current record is the one being changed if( id == inID ) { //seek to the current read position ioData.seekp( ioData.tellg() ); //output a space, then the new number ioData <<" " <<inNewNumber ; break; } //read the current number to advance the stream ioData >> number; } } 这段代码可以在一个已有的电话薄里把 ID=&