Number of bytes in an EOF?

This is a discussion on Number of bytes in an EOF? within the C++ Programming forums, part of the General Programming Boards category; I've been noticing the gap between characters read and the file size on my XP system with a loop I ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    11

    Number of bytes in an EOF?

    I've been noticing the gap between characters read and the file size on my XP system with a loop I just have go through a text file and read characters.

    Is EOF OS/Implementation specific? So far from what I calculated from the input filesize is 1210, the # of characters is 1188 which leaves 22 bytes unaccounted for. Here's the code

    Code:
    void rput_readfilev2(int a, _TCHAR * ar[])
    {
    	 int elapTicks;
         double elapMilli, elapSeconds, elapMinutes;
         clock_t Begin, End;             //initialize Begin and End for the timer
    
    	 /*
         Begin = clock() * CLK_TCK;      //start the timer  
         for(int a=1; a<=10000; a++);
         End = clock() * CLK_TCK;        //stop the timer 
         */   
         
        
    
    	ifstream fin;
    	ofstream fout;
    	long count2 =0;
    	long count;
    	long total = 0;
    	int ch;
    
    	int spaces=0;
    	int newlines=0;
    	int style =0;
    
    	// fout.open(ar[2]);  Open filename2(argument #2) for output
    	int file = 1;
    		
    	Begin = clock() * CLK_TCK;
    	
    	fin.open(ar[file]);
    
    
    	count = 0;
    	cout << file << " \n";
    	ch = fin.get();
    	while ( ch != EOF)  // While ch does not equal end of file, while quites if ch is EOF (end of file)
    	{
    		ch = fin.get();
    		/*cout << " 1. m " << count << "\n";*/
    		if (ch ==' ')
    			spaces++;
    		else if (ch == '\n')
    			newlines++;
    		cout << ch << "	    \n";
    		count++;
    	}
    
    	cout << "\n";
    	cout << "\nThere was " << count << " characters in \n" ;
    	wcout << "Argument 1 : " << ar[1] << " \n";
    	total +=count;
    	fin.clear();
    	fin.close();
    	// fout.close();  Closing the output file

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    When you open a file in "text" mode, OS-specific newline sequences are converted to a single '\n' (0xA in ASCII). Most Windows machines use the sequence [ 0xD, 0xA ] for newlines, so the first byte is simply discarded...
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    Also, while the underlying value of EOF is implementation-defined, the macro itself is portable. Furthermore, EOF is *not* a value stored in the file - it's just a value returned by file-manipulation functions as an indicator...
    Last edited by Sebastiani; 06-03-2010 at 04:03 PM. Reason: clarification
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number guessing game
    By kazoo in forum C Programming
    Replies: 7
    Last Post: 05-30-2010, 11:31 AM
  2. Convert bytes to int, keep the negative number
    By umm in forum C Programming
    Replies: 7
    Last Post: 03-20-2009, 04:47 PM
  3. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  4. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  5. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM

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