CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #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 )

  2. #2
    Join Date
    Jun 2002
    Location
    Maastricht / The Netherlands
    Posts
    79
    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) -

  3. #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.

  4. #4
    Join Date
    Jun 2002
    Location
    Maastricht / The Netherlands
    Posts
    79
    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) -

  5. #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...

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    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;
    }

  7. #7
    it´s working now

    ThX

  8. #8
    Join Date
    Apr 2002
    Posts
    388

    Lightbulb

    you also could have used your method with the opening mode "r+b"!

  9. #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
  •  





Click Here to Expand Forum to Full Width

Featured