Thread: Writting hex value into files

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    56

    Writting hex value into files

    I cant figure out how to make this damn code work.
    Its objective is simple: the user selects a file, type the offset and then the hex value he wants to be put in that specifit offset. This is what i came up with so far:

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        string fileName;
        int offset;
        string hexValue;
    
        cout<<"FILE: ";
        cin>>fileName;
    
        fstream myFile(fileName);
    
        if(myFile.is_open())
        {
            cout<<"OFFSET: ";
            cin>>offset;
    
            cout<<"HEX VALUE TO BE WRITTEN: ";
            cin>>hexValue;
    
            myFile.seekp(offset);
            myFile.write(hexValue,1);
        }
        else
        cout<<"Couldn't open file!"<<endl;
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    Bump

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well it sounds like you want to use a binary file to me. Put the whole file in a string, and edit it in the way you described. Then write() your new binary file when you're done.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    Well it sounds like you want to use a binary file to me. Put the whole file in a string, and edit it in the way you described. Then write() your new binary file when you're done.
    Isn't every file considered binary?
    Let's say I follow your advice and put the whole file in a string, how could i edit a specific character and replace it with a hex value (not a ascii character), how can this be done? (arent strings suppose to deal with ascii only?)

    btw thanks for answering

  5. #5
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Yes, all files saved on the hard drive in their most basic form are binary, but in this context we mean how you are opening the file. Using fstream you can open a file as a text stream or alternatively you can open it in binary format. I'm not going to go into the details of each of them because there are plenty of tutorials on the internet which discusses each 'mode' in detail.

    What I will say is that you are opening a file using fstream in text mode but then using write() as though it was a binary file. If you want to write a file in binary format you need to specify 'fstream::binary' when you use open it. (fstream open method reference). Then you would just save the data using the write() method.

    Another alternative is that you stick to keeping the file in text mode, in which case you could write the data in hex format by using using the overloaded operators, '<<' & '>>' to read and write to the stream but you would have to use the manipulator first so that it comes out in hexadecimal format.

    Click me for more information.

    Example code of using the operators to output in Hex:
    Code:
    n = 65;               /* If typecast to char, that would be the character 'A' */
    file << hex << n << std::endl;
    Last edited by Swarvy; 11-02-2010 at 12:32 PM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >>(arent strings suppose to deal with ascii only?)
    Yes and no. Yes, because you wouldn't be able to use unsigned chars in std::string without changing the char traits (which you can do, but vector<unsigned char> is also a comfortable alternative). No, because a string is a sequence of characters, one character == one byte, and unsigned char gives you the full range of values for a single byte. If you're using hexadecimal because you're working with bits, then that should be enough.

    If you have the whole file contained in an array like structure, then the offset is the subscript you would use.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    I seem to have another problem.

    this is how i set the mode flags in the constructor:
    Code:
    fstream myFile("file.bin",ios::in | ios::out | ios::binary);
    then:

    Code:
    myFile<<hex<<0xFF;
    the problem is, it writes FF in ascii (0x66) to the file when I coded it to write 0xFF!
    Why???
    Last edited by dhuan; 11-02-2010 at 03:59 PM.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It's probably just the modes again. In binary mode you are supposed to rely exclusively on read() and write(). The other method that Swarvey mentioned works for text mode.

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    It's probably just the modes again. In binary mode you are supposed to rely exclusively on read() and write(). The other method that Swarvey mentioned works for text mode.
    rely on read() and write()?
    i tried it and the compiler reports error message.

    Code:
            myFile.write(0xFF,1);
    error: invalid conversion from `int' to `const char*'|

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The first parameter to ofstream.write() is a const char * you are trying to write an integer. See this link write

    You might be better off using ofstream.put() See this link put. Which puts a char onto the stream.

    Jim

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    I still havent figured out how to write hex value to the file...

    here's what i tried:

    Code:
    myFile.put(0xFF,1);
    i only accepts char type data as argument...

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    You will need to cast the int 0xFF to a char * in order to use write.



    Code:
       int x = 0xFF;
       myFile.write ((char*)&x, sizeof (x));

    Jim

  13. #13
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    Another question, why can't the fstream constructor accept a string as the filename?

    Here's what I tried:

    Code:
        string fileName;
        cout<<"FILE:";
        cin>>fileName;
        fstream myFile(fileName,ios::in | ios::out | ios::binary);
    Compiler error:

    Code:
    no matching function for call to `std::basic_fstream<char, std::char_traits<char> >::basic_fstream(std::string&, std::_Ios_Openmode)'|

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dhuan
    Another question, why can't the fstream constructor accept a string as the filename?
    I cannot authoritatively state the reason why it was not provided with such a constructor, but you should write:
    Code:
    fstream myFile(fileName.c_str(), ios::in | ios::out | ios::binary);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jul 2010
    Posts
    56
    laserlight, thank you! I could never figure out that the solution to this problem was a simple c_str() function that trasnforms the c++ string into a dummie c char.

    Now, my next question is: how could i make translate a string into a hex value.
    I want to do something like this:

    Code:
    // ... opens a binary file
    
    string hexValue;
    cout<<"Hex value to be put in the file: ";
    cin>>hexValue;
    
    // translate hexValue string into REAL hex values
    
    myFile.write(hexValue);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  5. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM