Home > database >  Can not print last line of the file
Can not print last line of the file

Time:10-19

this is my code. It's a school project, we are not allowed to use fopen, just to let you know. I want to read the text file into a string, but the last line is not read. I think I know why, as I state, that whenever c == '\n' it prints a new line, but the last line has '\0' at the end. I just don't know how to fix it really.

#include <stdio.h>

struct contact
{
    char name[100];
    int number[100];
};

void read_lines()
{
    char line[100];
    char c;
    int i = 0;

    while ( (c = getchar()) != EOF)
    {
        if ( c == '\n')
        {   
            printf("%s", line);
            for (int j = 0; line[j] != '\0'; j  )
            {
                line[j] = '\0';     
            }
            i = 0;               
        }
        line[i] = c;
        i  ;
    }    
}

int main(int argc, char *argv[])
{   
    read_lines();

    return 0;
}

This is the text file:

Mark Brown
6846516516
Jane Black
6848489468
John White
8526848654

CodePudding user response:

I suspect your issue is not about reading the last line, but that the output of your program does not contain the final newline. If that interpretation is correct, it is easily fixed. Just write the newlines at the end of each line instead of at the start of the next. For instance:

int c;
char line[100];

while( (c = getchar()) != EOF && i < sizeof line - 1 ){
    line[i  ] = c;
    if( c == '\n' ){ 
        line[i] = '\0'; 
        fputs(line, stdout);
        i = 0;               
    }
} 

Note that this still does not handle long lines very well, but fixes the stated concern (if I interpret the stated concern correctly!).

Note also that I've changed the type of c. The value returned by getchar is an int, and you must not assign it to a char if you want to accurately distinguish EOF from valid data in the file.

Also note the bounds check on i. Lines in a text file can easily exceed 100 characters, and your program should not misbehave when that happens. (Although one could argue that simply terminating the loop on a long line is "misbehaving", it's not quite so bad as overflowing the bounds of the array.)

CodePudding user response:

You have to use an int variable to be able to meaningfully compare to EOF.
Also, you need to trigger the output for the last line, which does not have a trailing newline, on which your code currently triggers only.

To fix that make sure that the output can also be triggered by the EOF.
I.e. use the post-checking do-while and change the condition to also output on the EOF which is then available in the last iteration of the loop.

void read_lines()
{
    char line[100];
    int c;
    int i = 0;

    do 
    {
        c = getchar();
        if ((c == '\n')||(c==EOF))
        {   
            printf("%s", line);
            for (int j = 0; line[j] != '\0'; j  )
            {
                line[j] = '\0';     
            }
            i = 0;               
        }
        line[i] = c;
        i  ;
    }   while ( c != EOF) ;
}

Output in what I hope is a correct reproduction of your problem:

Mark Brown
6846516516
Jane Black
6848489468
John White
8526848654

CodePudding user response:

getchar will not be useful for your project as it reads from the standard input stdin.

You should have a look at the read function.

  •  Tags:  
  • c
  • Related