Thread: Hopefully simple question, input streams

  1. #1
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250

    Hopefully simple question, input streams

    Hopefully this is a simple question. I searched out for it, but found nothing. Basically I just need to know some simple info.

    Basically I am reading in a file in binary using ifstream. All is fine and good, then I hit some spaces, and then it gets all wacky from there.

    I setup a char array of 4 bytes and read in 4 bytes at a time. This works for a while until I hit a part of the file and then it starts to read spaces and messes up what I am reading.

    So for example:

    ...

    Code:
    char *tempPtr;
    char tempVal[4];
    
    in.read( tempVal, 4 );  //say for instance a line in hex 34F2
    //format the string into hex
    long value = strtol(  tempVal, &tempPtr, 16 );
    then it hits a line with a space, ( I can see the raw file as well, and in a hex editor it ignores these, so I can assume there is a way to decide what is a is not a space / \n), and then gets a little funky...

    same code as before, on a particular 8 bytes...

    Code:
    char *tempPtr;
    char tempVal[4];
    
    in.read( tempVal, 4 );  //line is 0000 0106 0DEE 05AB
    //format the string into hex
    CString temp;
    temp.Format( "%02X%02X%02X%02X", tempVal[0], tempVal[1], tempVal[2], tempVal[3] );  
    
    in.read( tempVal, 4 );  // read in the second part of the above line
    //format the string into hex
    CString temp1;
    temp1.Format( "%02X%02X%02X%02X", tempVal[0], tempVal[1], tempVal[2], tempVal[3] );  
    
    CString joined = temp + temp1;

    This should come out as normal, but when I hit the spaces it comes out more like this...

    0000 0106 0DFFFFEEFFFF05AB

    which to me seems impossible, but hey, I am no expert either .

    Any simple mistakes anyone sees with that? I am hesitant to eradicate all FF's ( space in ascii ) because there are of course numbers which have FF in them. Any ideas on this? I am kinda not able to spot it, but perhaps someone has a good way to id them. Thanks!
    Last edited by dpro; 03-08-2006 at 10:41 PM. Reason: Well it would help to get the code correct it would seem!

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    looks to me like you wrote the file yourself... bad times if you're really reading it in as binary...

    ...and for some reason those reads look wrong...
    Last edited by major_small; 03-08-2006 at 10:40 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    No I didn't write it myself. I can hex edit it, its generated and readable in a separate program, so I know this one is "correct". And yes it is written as a binary file, 'tis how they decided to format the damn thing . The reads are correct according to the standard, unless I somehow misread it...

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well if it were a true binary file, there should be no problem with spaces... you have to read it in the same way you wrote it out, unless you're paying close attention to what you're doing to it.

    In other words, I need to see how it was written to see what's wrong with how it's being read =P
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Haha well I can post the file when I get to the computer with it. This was a file written via a java applet, that is, using DataInputStream.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well that could be your problem...

    I think you meant the file was written with DataOutputStream, which, by definition, outputs something meant to be read in by DataInputStream. How exactly it does that depends on Sun's implementation.

    you need to know exactly how Sun wrote the data out to the file, which means you need to remember what method you used to write it out, and then in C++ figure out how sun's data types differ from your platform's data types and make the conversion.

    for example, writeBytes(String s) writes each character as one Byte discarding the high-order bits, whereas writeChars(String s) writeas each character as two Bytes. On my system, each character is saved in one Byte. Therefore, if I used writeChars(String s), I'd have to discard the upper 8 bits somehow (possibly with a double-read). I'd also have to make sure all the data was packed tight, with no empty spaces (because that's how fstream reads binary files)

    the point is: DataOutputStream outputs data in a way that can be read by DataInputStream, but that doesn't necessarily mean it's a true binary file. Any spaces in a real binary file are data that the text editor is trying to display in an incompatible character set (ASCII).

    Here's some code that writes and then reads a binary data file in C++:
    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
    	char abc[7]="ABCDEF";
    	double num=3.14159;
    	std::fstream file("test.dat",std::ios::out|std::ios::trunc|std::ios::binary);
    	
    	file.write(abc,sizeof(char)*7);
    	file.write(reinterpret_cast<char*>(&num),sizeof(num));
    	
    	file.close();
    	file.clear();
    	abc[0]='\0';
    	num=0.0;
    
    	file.open("test.dat",std::ios::in|std::ios::binary);
    	file.read(abc,sizeof(char)*7);
    	file.read(reinterpret_cast<char*>(&num),sizeof(num));
    	file.close();
    
    	std::cout<<abc<<std::endl<<num<<std::endl;
    	return 0;
    }
    and here's what the data file looks like in GVIM:
    Code:
    ABCDEF^@n~F^[πω!	@
    (the blue characters are control characters, and really shouldn't be visible)
    Last edited by major_small; 03-08-2006 at 11:50 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by dpro
    Reading 0000 0106 0DEE 05AB

    This should come out as normal, but when I hit the spaces it comes out more like this...

    0000 0106 0DFFFFEEFFFF05AB

    which to me seems impossible, but hey, I am no expert either .
    EE is a negative value and is printing as FFFFEE. Make the variable tempVal unsigned and it should print properly.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    I would agree with you major_small, except it is completely readable. hex workshop reads the values correctly every time it loads the file. This would indicate that it should work.

    WaltP, that seemed to work, it is reading in the correct value. Thanks, I will see how this works for the rest and go from there, thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. 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
  3. Very very simple file input question.
    By Kurisu33 in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2006, 11:37 PM
  4. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM