C++/File/File Write
Change file content
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
if(argc!=4) {
cout << "Usage: CHANGE <filename> <byte> <char>\n";
return 1;
}
fstream out(argv[1], ios::in | ios::out | ios::binary);
if(!out) {
cout << "Cannot open file.\n";
return 1;
}
out.seekp(atoi(argv[2]), ios::beg);
out.put(*argv[3]);
out.close();
return 0;
}
Create output file
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream fout("test");
if(!fout) {
cout << "Cannot open output file.\n";
return 1;
}
fout << "Hello!\n";
fout << 100 << " " << hex << 100 << endl;
fout.close();
ifstream fin("test"); // open input file
if(!fin) {
cout << "Cannot open input file.\n";
return 1;
}
char str[80];
int i;
fin >> str >> i;
cout << str << " " << i << endl;
fin.close();
return 0;
}