fgetc - Extra newline

This is a discussion on fgetc - Extra newline within the C Programming forums, part of the General Programming Boards category; When reading characters from a file using fgetc, it seems to read an extra newline character at the end of ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    fgetc - Extra newline

    When reading characters from a file using fgetc, it seems to read an extra newline character at the end of the file even when a text editor (GVim) doesn't show one there. This happens on a Unix machine, but I don't have the problem when I run it in Windows. Anybody know why?

    Here's the code:

    Code:
    int currChar = 'a';
    char buffer[256];
    
    FILE *input;
    
    // ...
    
    // Process each line of the input file
    while (currChar != EOF)
    {
    	int i;
    
    	// Read line
    	for (i = 0; (currChar = fgetc(input)) != '\n' &&
    			currChar != EOF;			i++)
    	{
    		buffer[i] = currChar;
    	}
    
    	buffer[i] = '\0';
    
    	// Output each line
    	printf("Buffer (%i): %s\n", i, buffer);
    }

    The input text file:

    Code:
    abc
    x
    y
    z

    The Unix output:

    Code:
    Buffer (3): abc
    Buffer (1): x
    Buffer (1): y
    Buffer (1): z
    Buffer (0):

    The Windows output:

    Code:
    Buffer (3): abc
    Buffer (1): x
    Buffer (1): y
    Buffer (1): z

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    982
    There is a newline at the end of your file. Vim is just assuming (and smartly, I think) that you'll want a newline at the end of all lines of a text file. You can change this with:
    Code:
    :set binary noeol
    That should allow you to save a file without the final newline.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    No it's not reading an extra newline character; you sure you need the while loop? and NULL terminating the line should be done conditionally.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    Quote Originally Posted by itCbitC View Post
    No it's not reading an extra newline character;
    What's it doing, then?

    Quote Originally Posted by itCbitC View Post
    you sure you need the while loop?
    What else would I use?

    Quote Originally Posted by itCbitC View Post
    and NULL terminating the line should be done conditionally.
    What would the condition be? I don't see why that would be necessary. If it's to ensure that i is less than 256, that's not necessary since this is a homework assignment and we can assume the length of the line is less than 256 characters.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    Quote Originally Posted by cas View Post
    There is a newline at the end of your file. Vim is just assuming (and smartly, I think) that you'll want a newline at the end of all lines of a text file. You can change this with:
    Code:
    :set binary noeol
    That should allow you to save a file without the final newline.
    This is a homework assignment, so I don't really have control over the input files.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Trace the loop when currentChar is EOF you will see why the NULL terminating and printing the string should be conditional as in.
    Code:
    if (currChar != EOF) {
        buffer[i] = '\0';
        printf("Buffer (%i): %s\n", i, buffer);
    }
    Whoops! you do need the while loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. buffer has extra contents in gtk+ 2.0
    By MK27 in forum Linux Programming
    Replies: 5
    Last Post: 08-04-2008, 11:57 AM
  2. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05:33 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  4. From where these two extra bytes came?
    By Juganoo in forum C Programming
    Replies: 4
    Last Post: 12-24-2002, 04:32 PM
  5. fgets and a bothersome newline char
    By ivandn in forum Linux Programming
    Replies: 1
    Last Post: 11-14-2001, 12:41 PM

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