Thread: fgets new line at end

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174

    fgets new line at end

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_COL 70
    #define MAX_ROW 20
    
    
    int main(void) {
        FILE *fp;
        char *text;
        char line[MAX_COL];
        int i;
        
        fp = fopen("input.txt", "r");
        if (fp == NULL) {
            fprintf(stderr, "!!! Failed to open file\n");
            abort();
        }
        
        text = malloc(MAX_COL*MAX_ROW*sizeof(char));
        
        for (i=0; i<MAX_ROW; i++) {
            if ((fgets(line, MAX_COL, fp)) != NULL)
                strcat(text, line);
        }
        
        printf("%s", text);
    
        return EXIT_SUCCESS;
    }
    With my input.txt file being

    Code:
    abcd
    efgh
    And in particular, there is no new line after the letter h, but when I print out the text string, I get a new line after h. Why is this?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you *sure* there's no new line after the letter h? Just because there's no text on the next line doesn't mean the file doesn't end with a new-line character.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Your editor is probably adding it by default. If you're on Linux or the like, that is very probable. Not sure if it's a POSIX standard thing or if the editor is doing it to "be safe" since some programs wont behave so well if it's not there. Try hexdump -C input.txt and look for a 0a byte at the end. Your editor may have an option to disable that feature.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    You can check if the last character is a newline by checking this condition

    Code:
    (len = strlen(line)) > 1 && line[len-1] == '\n'

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Don't assume that memory you get from malloc is filled with zeros.
    If the first byte isn't \0 when you come to do the strcat calls, you're stuck.
    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.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    174
    Oh I see. The new line isn't a big deal for me in this case, I was just worried that I was doing something wrong.

    Quote Originally Posted by Salem View Post
    Don't assume that memory you get from malloc is filled with zeros.
    If the first byte isn't \0 when you come to do the strcat calls, you're stuck.
    Right, thanks. I'll go ahead and add

    Code:
    text[0] = '\0'
    after I malloc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets - how to get a line length
    By baxy in forum C Programming
    Replies: 3
    Last Post: 04-21-2013, 04:30 PM
  2. Skipping one line using fgets
    By lveenis in forum C Programming
    Replies: 3
    Last Post: 11-25-2011, 03:45 PM
  3. fgets reading the same line twice
    By lukeaar in forum C Programming
    Replies: 3
    Last Post: 03-25-2010, 06:50 AM
  4. fgets and skipping a line...
    By pollypocket4eva in forum C Programming
    Replies: 3
    Last Post: 01-03-2009, 01:42 PM
  5. fgets and new line
    By 9988776655 in forum C Programming
    Replies: 4
    Last Post: 01-05-2008, 12:57 PM