Thread: Extracting from stream issue

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    81

    Extracting from stream issue

    Hello, I can't wrap my head around why its possible I'm getting different lengths of output.

    Code:
       while ( (file >> byte) && (bytesRead < dataBlockSize) ) 
       {
          ss << hex << setfill('0') << setw(2) << byte; 
          bytesRead++; 
       }
       // bytesRead is always 16 unless its the last part of the file.
    #ifdef DEBUG
                cout << "||||" << bytesRead << "||||" << endl;
    #endif 
       cout << ss.str() << endl;
    I get output that differs in length like this for example:
    Code:
    |||| 16 ||||
    dddddddddddddddddddd
    |||| 16 ||||
    ddddddddddddddddddddddddd
    |||| 16 ||||
    dddddddddddddddddd
    |||| 16 ||||
    ddddddddddddddddddddd
    |||| 16 ||||
    ddddddddddddddddddddd
    |||| 16 ||||
    ddddddddddddddddddddd
    The only time this should be possible is if bytesRead is also a different number. Except bytesRead is always the same number.
    Last edited by keira; 02-09-2008 at 08:46 PM.

  2. #2
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    Actually making the byte an unsigned char rather than a char +

    ss << hex << setfill('0') << setw(2) << (int) byte;

    changing that line to that seems to have solved it. Can anyone comment on why?

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    OK, I'm a bit lost... What does the actual file look like?
    Did you open the file in Text mode or Binary mode?
    What does dataBlockSize equal? I'm assuming bytesRead is 0 before this loop?

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    The file is opened in binary mode. dataBlockSize equals 16 in this case, but it shouldn't matter what it equals so long as bytesRead is the same value when it breaks out.

    I never noticed this bug when the file was a text file (opened in binary mode). Since I tried it on a non-text file I am getting these different lengths of output. I realized that by casting it to int i'm getting a uniform length of output but its incorrect output since its giving me 4 times as much data as i need (cause an int is four bytes in size), I would have thought setw(2) would eliminate that issue but I don't know what I'm talking about.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    setw only sets a minimum width, never a maximum one. (IOW, C++ will never truncate an integer for that.)

    Were you losing any of your data, as compared with the original? Gaining data? Neither, but it was just in odd chunks?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stream Server
    By ch4 in forum Networking/Device Communication
    Replies: 18
    Last Post: 06-29-2009, 03:09 PM
  2. Discard wrong data from stream
    By mesmer in forum C Programming
    Replies: 7
    Last Post: 11-16-2008, 02:30 AM
  3. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  4. Closing a stream
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2008, 05:15 PM
  5. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM