Streaming file input problem

This is a discussion on Streaming file input problem within the C++ Programming forums, part of the General Programming Boards category; This is one of the reasons Prelude stresses portability so much. Portability? I'm using the same system to read and ...

  1. #16
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    This is one of the reasons Prelude stresses portability so much.
    Portability? I'm using the same system to read and write.
    Because you are on a little endian machine. Let me guess, a PC?
    Yes, I'm on a pc. After reading a little bit about endians, I think it has to do with the cast. I think the reason I don't get 12,594 for the output is that the cast assumes a certain endian, and if your system does not employ that endian, then things get flipped around. Therefore, the cast takes this in the file:

    00110001 00110010

    and flips it around to this:

    00110010 00110001

    which is the number 12,849
    Last edited by 7stud; 07-28-2005 at 02:52 PM.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >   ofstream outputFile("input.bin", ios::trunc);
    Oops, and also open the file in binary mode. Both when you do the writing, and when you do the reading.
    Code:
       ofstream outputFile("input.bin", ios::binary | ios::trunc);

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Portability? I'm using the same system to read and write.
    In that case you're fine.

  4. #19
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    In that case you're fine.
    Well, no. My program displays 12,849 instead of the expected 12,594. I think the problem is that a cast somehow reverses the bytes.
    Last edited by 7stud; 07-28-2005 at 03:14 PM.

  5. #20
    Registered User
    Join Date
    May 2005
    Posts
    22
    Quote Originally Posted by swoopy
    Oops, and also open the file in binary mode. Both when you do the writing, and when you do the reading.
    Code:
       ofstream outputFile("input.bin", ios::binary | ios::trunc);
    Yeah I had the ios::binary in there in the first place but it made no difference.

  6. #21
    Registered User
    Join Date
    May 2005
    Posts
    22
    Quote Originally Posted by swoopy
    Then you should use write() to output your data.
    [code
    int main(int argc, char* argv[])
    {
    unsigned short int num;

    ofstream outputFile("input.bin", ios::binary | ios::trunc);
    for(num = 100; num < 200; num++)
    {
    outputFile.write( reinterpret_cast<char*> (&num), sizeof(num) );
    cout << "added " << num << "...\n";
    }
    return 0;
    }
    [/code]
    Use write() and read() for binary I/O.
    BINGO!! You got it swoopy! I was writing the file using << instead of write(...) so even though I had switched my input function to use read(...) I was still reading an improperly created file.

    Thanks everyone for your help!

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. file input output problem..
    By epidemic in forum C++ Programming
    Replies: 10
    Last Post: 12-03-2006, 03:55 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. File input problem
    By robert_sun in forum C Programming
    Replies: 1
    Last Post: 05-14-2004, 06:54 AM

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