Thread: Fstream limit?

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    206

    Fstream limit?

    Code:
    int main()
        {
            BYTE Header[500];
            fstream file_op("C:\\Test.exe",ios::in);
    		file_op.getline(Header,500);
    		for(int i = 0; i < 150; i++)
    			cout<<"\n"<<hex<<(int)Header[i];
            return 0;
    }
    The first 118 bytes are correct, but all following bytes are 0xCC.

    It reports bytes 115-120 are: 2E 0D 00 CC CC
    The real bytes 115-120 are: 2E 0D 0D 0A 24

    Is there a limit, or does anybody know what I'm screwing up?

  2. #2
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    Yes, you screwed up.
    Open the file in binary mode.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    getline() stops reading after it encounters '\n' or end of file, it does not alter contents of the remaining bytes in the buffer. The 0xcc that you see after the 118th byte was probably the way your compiler initialized the buffer. VC++ 6.0 will do that when the program is compiled in debug mode, I don't know buf I suspect VC++ .NET will too. If you don't want that behavior then initialize the buffer yourself
    Code:
      BYTE Header[500] = {0};

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fstream. I/O
    By kevinawad in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2008, 09:19 AM
  2. ARGH! fstream errors
    By OttoDestruct in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2004, 10:37 PM
  3. Replies: 3
    Last Post: 01-08-2004, 09:43 PM
  4. hi need help with credit limit program
    By vaio256 in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2003, 12:23 AM
  5. Are fstream and ofstream incompatible?
    By johnnyd in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2003, 12:21 PM