Writting hex value into files

This is a discussion on Writting hex value into files within the C++ Programming forums, part of the General Programming Boards category; I cant figure out how to make this damn code work. Its objective is simple: the user selects a file, ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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 01:32 PM.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    >>(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 04:59 PM.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    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
    2,919
    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
    2,919
    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
    22,303
    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);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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);

Page 1 of 2 12 LastLast
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, 08: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, 10: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, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21