i have writing program which takes file name(full path) and do some modification on these file.
Assuming we have a file that ready to work.
file.txt:
name id age
name id age
name id age
name id age
if i wanna change the id to specific string what function should i use?
My code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main(){
ifstream file("file.txt");
string buffer;
getline(file, buffer);
/*
Here i will change buffer's content to do( modification )
*/
cout<<buffer<<endl; // print out our new data
file.close();
return 0;
}
Read in the information, change it to what you want it to be, and re-output it into the file. In case you didn't know the syntax for outputting data to a file is 'ofstream name << "Your string or data here" ofstream name is the name of the ofstream (output file stream) you declare. For instance:
1 2
ofstream uselessInfo("Example.txt");
uselessInfo << "This is a string";