Thread: hex writing

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    56

    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
    if x == y , y == x

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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]
    Last edited by hk_mp5kpdw; 01-19-2005 at 12:54 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    56
    hehe thx that was the problem
    if x == y , y == x

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing hex to a file
    By koxson in forum C++ Programming
    Replies: 8
    Last Post: 05-11-2009, 09:49 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Writing 64 bit value in hex to a string
    By rahulgbe in forum C Programming
    Replies: 2
    Last Post: 03-18-2006, 01:15 PM
  4. hex writing stuff
    By gamer in forum C++ Programming
    Replies: 3
    Last Post: 01-04-2005, 05:50 AM
  5. Replies: 1
    Last Post: 04-05-2002, 11:19 AM