File is skipping?

This is a discussion on File is skipping? within the C++ Programming forums, part of the General Programming Boards category; My program is supposed be reading a table that has two numbers: 4 byte integer to the start of the ...

  1. #1
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902

    File is skipping?

    My program is supposed be reading a table that has two numbers: 4 byte integer to the start of the data within the file, and 4 byte integer of the data's size. The file looks like this (in hex) in the problem area:
    Code:
    0D000000 48050100
    I call the same function twice, void IO_Read32(std::ifstream &s, unsigned int &x);
    Code:
    		IO_Read32(infile, data_off);
    		IO_Read32(infile, data_size);
    Code:
    void IO_Read32(std::ifstream &s, unsigned int &x)
    {
    	unsigned char c;
    	int i;
    	
    	x = 0;
    	
    	for(i = 0; i < 4; ++i)
    	{
    		std::cout << "Now at " << s.tellg() << std::endl;
    		s >> c;
    		x |= c << (i * 8);
    	}
    }
    The cout line is for debugging, I had originally thought I had seek()'d one byte too far. This is not the case, as that line revealed. The program writes:
    Code:
    Now at 5171581
    Now at 5171583
    Now at 5171584
    Now at 5171585
    Now at 5171586
    Now at 5171587
    Now at 5171588
    Now at 5171589
    Notice the lack of byte 5171582! It doesn't read that byte, causing all of the following reads to be one byte off. Why?
    The first byte is a 0x0D, so I was thinking newlines, but the file is opened for binary input. (It's not a text file.) I open the file:
    Code:
    infile.open(filename.c_str(), std::ios::binary | std::ios::in);
    infile.is_open() returns true afterwards. Does this have something to do with the way I'm reading the file, and is there some better way to do single byte reads?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    s >> c;
    You haven't initialized variable c.

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Initializing it doesn't (nor should?) change the output.
    s >> c; is supposed to read a single byte in from s and store it in c, right? c should be the value of the file, unless some error occurs perhaps. (Which doesn't seem to, as it keeps reading and the error checks prove false) It's initialized to zero now anyways.

    EDIT:
    Follows is a full program demostrating the problem:
    Code:
    #include <iostream>
    #include <fstream>
    
    int main(int argc, char *argv[])
    {
    	std::ifstream ifs;
    	unsigned char c = 0;
    	int i = 0;
    
    	if(argc != 2)
    	{
    		std::cerr << "Need a file to read." << std::endl;
    		return 1;
    	}
    	
    	ifs.open(argv[1], std::ios::binary | std::ios::in);
    	if(!ifs.is_open())
    	{
    		std::cerr << "Failed to open file " << argv[1] << "." << std::endl;
    		return 1;
    	}
    	
    	while(1)
    	{
    		ifs >> c;
    		if(ifs.eof()) break;
    		i = c;
    		std::cerr << "Read value " << i << " from position " << ifs.tellg() << "." << std::endl;
    	}
    	
    	ifs.close();
    	
    	return 0;
    }
    Compiled with:
    Code:
    g++ -Wall -o fileskip.exe fileskip.cpp
    Run on file "fileskip.dat", contents (in hexadecimal) are as follows:
    Code:
    00 05 0D 00 50 0A 00
    Code:
    C:\...>fileskip fileskip.dat
    Read value 0 from position 1.
    Read value 5 from position 2.
    Read value 0 from position 4.
    Read value 80 from position 5.
    Read value 0 from position 7.
    Skips both 0A and 0D (the bytes used for newlines). Why?
    Compiled with gcc version 3.4.2 (mingw-special)
    All I need is a method of reading 1 byte... the C++ equivalent of C's fgetc();.
    Last edited by Cactus_Hugger; 06-02-2006 at 10:42 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Did you try one of the overloaded get() member functions?
    Code:
    //ifs >> c;
    c = ifs.get();
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Cactus_Hugger
    s >> c; is supposed to read a single byte in from s and store it in c, right?
    By default, the extraction operator skips whitespace (0x0d, 0x0a, 0x09, 0x20).

    You could try noskipws:

    Code:
       s >> noskipwhitespace >> c
    But s.get() or s.read() would be my preference for reading binary files.

    D
    Last edited by Dave Evans; 06-02-2006 at 11:31 PM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    By default, the extraction operator skips whitespace (0x0d, 0x0a, 0x09, 0x20).
    Actually, no. It skips anything that isspace (in <cctype>) returns true for, which is in the C locale, space, tab, formfeed carriage return, and vertical tab: http://www.mkssoftware.com/docs/man3/isspace.3.asp.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by dwks
    Actually, no.
    OK.

    And, while we're at it, we might as well point out that the manipulator is "noskipws", not "noskipwhitespace".

    Code:
      cin >> noskipws >> c;

    D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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