|
-
June 12th, 2002, 06:45 AM
#1
how to change single chars by overwriting
hi
i have a problem by writing into a file in binary mode...
i want to overwrite single characters in given positions...
so far i´m using that code:
FILE *out;
out = fopen(<filename>, "ab"); // a for append, b for binary
fseek (out, <intPosition>, SEEK_CUR);
fputc(<char>, out);
the problem with this technique is, that the code doesn't overwrite existing characters, but appends everything at the end of the file (although i have set the filepointer to the correct position)...
using "wb" instead of "ab" when opening the file results in overwriting the whole file...
does anybody know a correct (and maybe quick) way to handle this problem ?
thx a lot
nichtswissender (<-- knownothingman )
-
June 12th, 2002, 07:07 AM
#2
Why don't you just read the file in to the memory and change it and write it to disk like a new file?
That would be a simple sollution....
The search feature of this forum is on the top right of the page. It might be a very good idea to use it.
----------------
To retain respect for sausages and laws, one must not watch them in the making.
- Otto von Bismarck (1815-1898) -
-
June 12th, 2002, 07:19 AM
#3
because i have to change up to 600 files. inc. database-files which can have up to 80MB. And i only have to change a few bytes per file.
Last edited by nichtswissender; June 12th, 2002 at 07:21 AM.
-
June 12th, 2002, 07:33 AM
#4
I don't know which development envirement you are using, but Visual C++ has the CFile class that has a read-write mode...
I don't know what facilities ansi C++ has
The search feature of this forum is on the top right of the page. It might be a very good idea to use it.
----------------
To retain respect for sausages and laws, one must not watch them in the making.
- Otto von Bismarck (1815-1898) -
-
June 12th, 2002, 07:37 AM
#5
i'm working on Borland Builder...
the FILE * object works similar to the CFILE * object from visual...
there also exists the read-write mode, but when i open the file in write-mode, the whole file gets overwritten... when i open the file in append-mode, everything is appended at the end of the file...
-
June 12th, 2002, 11:35 AM
#6
Maybe this will do what you want
Code:
#include <iostream>
#include <fstream>
#include <ios>
int main()
{
std::fstream myfile("myfile.dat",std::ios::in|std::ios::out|std::ios::binary);
myfile.seekg(0); // goto start of file
myfile.put('c'); // and replace it with 'c'
myfile.seekg(8); // goto the ninth character in file
myfile.put('Q'); // and replace it with 'Q'
myfile.seekg(0,std::ios::end); // goto the end of the file
// start appending as usual
myfile.write("append",6);
return 0;
}
-
June 13th, 2002, 02:03 AM
#7
it´s working now
ThX
-
June 13th, 2002, 02:54 AM
#8
you also could have used your method with the opening mode "r+b"!
-
June 13th, 2002, 03:12 AM
#9
so... here is the next problem...
when i have edited the files i have to cut off a few bytes at the end.
how can i delete the last few bytes? at the moment i only can overwrite them and I can´t find a way to delete them after i have set the EOF byte '\0'.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|