Search code examples
cfileiocursorprintf

Why cursor goes to next line while printing a file of one line?


I have below C program to print the content of a file and to count total number of characters in it.

#include<stdio.h>
#include<stdlib.h>

int main()
{
    /*Declaring a FILE pointer fp.*/
    FILE *fp;
    char ch;
    int noc = 0;

    /*Using fopen() to get the address of the FILE structure, and store it in fp*/
    fp = fopen("poem.txt","r");
    if (fp == NULL)
    {
            printf("Error opening file");
            exit(1);
    }
    while(1)
    {
            ch = fgetc(fp);
            if (ch == EOF) /* EOF will be there at the end of the file.*/
                    break;
            noc++;
            printf("%c",ch); /* Printing the content of the file character by character*/
    }

    //printf("\n");

    close(fp); /* We need to fclose() the fp, because we have fopen()ed */

    printf("\tNumber of characters: %d\n",noc);

    return 0;
}

/*One interesting observation: There is an extra new line printed. Why ???*/

File poem.txt has just one line. Below is content of poem.txt

It starts with the end.

I have not hit ENTER while writing this file so it is only of one line.

-bash-4.1$ wc poem.txt
 1  5 24 poem.txt
-bash-4.1$

You can see this is confirmed by wc (Howevr I still dont understand why wc gives number of characters as 24, instead of 23).

Below is the output of this program.

-bash-4.1$ ./a.out
It starts with the end.
     Number of characters in this file: 24
-bash-4.1$

You can see that after printing all the characters of the file, the cursor is taken to the next line even though I have commented the printf("\n") after printing all the characters of the file .

Why is the cursor taken to new line? I was expecting cursor to be in the same line after printing all the characters of the file, hence used a \t in the next printf.

Also you see that my program says that number of characters is 24 (which is inline with "wc poem.txt" output).

So I am confused why is the cursor taken to new line after printing last character of the file ? Also why is total number of characters (noc) 24 and not 23 ?

P.S. Although I have same question for the number of characters shown by "wc" you can ignore the "wc" outout except the number if lines. I may probably post a next question for that.

Thanks.


Solution

  • Why is the cursor taken to new line?

    • Because the '\n' character is in the file.

    Also you see that my program says that number of characters is 24 (which is inline with "wc poem.txt" output).

    • Because the 23 characters that were printed plus the '\n' are all in the file.

    You can try to escape the whitespace characters to see them, since they are invisible, so something like this

    while ((ch = fgetc(fp)) != EOF)
    {
        noc++;
        if (isspace(ch) != 0)
            printf("\\%02X", ch);
        else
            printf("%c", ch);
    }
    

    this way you will see each character, you will need to include <ctype.h>

    Note: using break is IMHO only needed in very special situations, I don't like it because it makes hard to follow the program flow.