Thread: newlines and getc()... i just dont get it.

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

    newlines and getc()... i just dont get it.

    Hi, I'm getting two spaces instead of just one and this is not good!

    The differences occur when there is just a blank line.

    So if we had a file:
    Code:
    /* everything is good */
    /* everything is good */ 
    
    /* I'm a space ahead of where I am now if you output me with the below code */
    Here's the code:
    Code:
     
    	while ( EOF != (inChar = getc(inFile)) ) {
    		if ( (column >= options.width) ) {
    			if ('\n' != outLine[offset-1] ) { 
    				outLine[offset++] = '\n'; 
    			}
    			fwrite(outLine, sizeof(char), offset, stdout); 
    			memset(outLine, '\0', 65535); 
    			offset = lastBlank = thereWasASpaceInThisLine = column = 0;
    		}
    
    		outLine[offset++] = inChar;
    		column = incColumn(&column, inChar, options);
    
    		if ( '\n' == inChar ) {
    			thereWasASpaceInThisLine = FALSE;
    			column = options.width;
    		}
    	}

  2. #2
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    Perhaps... it has to do with the linux newline being 0d0a0d? My application could be incorrectly interpretting that as two newlines. (From looking at the hex, I"m seeing 0d0a0d)

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    81
    I still can't figure this one out. Is it recommended to just read the file in binary and forget about this '\n' stuff? I just want to output text exactly as how it exists in the file, and the only problem I'm encountering is sometimes it will output 2 new lines where there should only be one.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I cant really understand what are u trying to do. Are u trying to just print what ever there in the file line by line?

    If that was the case why dint use fgets. And perhaps can you please explain and post a bit more of code so that i could run it on my machine and see what i get and debug.

    ssharish

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    fgets/fputs would be easier to read and write a text file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Perhaps... it has to do with the linux newline being 0d0a0d?
    That looks more like a DOS file which was accidentally edited on Linux (or vice-versa).

    Linux would be just 0A
    DOS/Win32 would be 0D0A

    There are a couple of Linux utilities, dos2unix and unix2dos, which can clean up the line endings and make them more suited to one OS or the other.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed