writing 10 gives 10 and 13

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thepenguin77
    New Member
    • Jul 2009
    • 4

    writing 10 gives 10 and 13

    I am trying to write numbers to a binary file. The problem that I have is when I try to write a 10 it is preceded by a 13. I believe that this has something to do with the program thinking that I want to put in a return.

    When I write:

    newFile.put(26) ;
    newFile.put(10) ;
    newFile.put(0);

    and hex dump. The result is:
    1A 0D 0A 00
    That 0D shouldn't be there.

    Is there a better way to write numbers to file or a way to stop this from happening? Also I need the numbers in this order since a different program will be using the file.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by thepenguin77
    I am trying to write numbers to a binary file. The problem that I have is when I try to write a 10 it is preceded by a 13. I believe that this has something to do with the program thinking that I want to put in a return.

    When I write:

    newFile.put(26) ;
    newFile.put(10) ;
    newFile.put(0);

    and hex dump. The result is:
    1A 0D 0A 00
    That 0D shouldn't be there.

    Is there a better way to write numbers to file or a way to stop this from happening? Also I need the numbers in this order since a different program will be using the file.
    I'll assume that you are using ofstream objects, if so put takes a char as a argument,not an int.If you have opened your file in binary mode,just use << operator to write your data into the file.

    ,kind regards

    Comment

    • thepenguin77
      New Member
      • Jul 2009
      • 4

      #3
      Ok, So I would use
      newFile << 26 << 10 << 0;

      And I'm using fstream if it makes a difference.

      Edit: It didn't work, I opened it with ios::binary and when I used << it just put the ascii code in the file.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by thepenguin77
        Ok, So I would use
        newFile << 26 << 10 << 0;

        And I'm using fstream if it makes a difference.

        Edit: It didn't work, I opened it with ios::binary and when I used << it just put the ascii code in the file.
        The operator<< formats the values you write; use the put() or the write() functions for unformatted output. Have a look here.

        kind regards,

        Jos

        Comment

        • thepenguin77
          New Member
          • Jul 2009
          • 4

          #5
          I found the problem. When I was using write() and put() I didn't have it set to binary.

          Comment

          Working...