-
hex writing
hi,
im trying to keep the file but edit a offset so i use ios::app to get the file, now i try to use a_file.seek to get the offset and write with put only it puts it to the end of the file and not to the given offset and in ascII can some1 help me with this? my code is
Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <ostream>
using namespace std;
int main(int argc, char *argv[])
{
char blah = 0x2;
ofstream a_file ( "c:\\test.txt ", ios::app);
if ( !a_file.is_open() ) {
cout<<"error opening file.";
}
else {
cout<<"file opened";
a_file.seekp(0x2);
a_file.put(74);
a_file.flush();
a_file.close();
}
system("PAUSE");
return EXIT_SUCCESS;
}
i use dev-c++ 4.9.9.1
edit: removed unused code
-
Files opened in append mode automatically seek to the end of the file whenever any writes are made. Thus you are not able to write anywhere within the file prior to the end-of-file. You probably want to open an fstream object in in/out mode:
Code:
fstream file("file.txt",ios::in|ios::out);
[edit]Didn't see that ASCII part of your question. If I understand what you're be saying, you might also need to open your file using ios::binary as well as the other options mentioned.[/edit]
-
hehe thx :D that was the problem