为什么在打印一行文件时光标会转到下一行?

时间:2021-05-02 23:27:40

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

我在C程序下面打印文件的内容并计算其中的总字符数。

#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

文件poem.txt只有一行。以下是poem.txt的内容

It starts with the end.

它从结束开始。

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

我写这个文件时没有按ENTER键,所以它只有一行。

-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).

你可以看到这是由wc确认的(Howevr我仍然不明白为什么wc给出的字符数为24,而不是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 .

您可以看到,在打印完文件的所有字符后,即使我在打印完文件的所有字符后注释了printf(“\ n”),光标也会转到下一行。

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.

为什么光标被带到新线?我打算在打印文件的所有字符后光标位于同一行,因此在下一个printf中使用\ t。

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

你也看到我的程序说字符数是24(与“wc poem.txt”输出内联)。

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 ?

所以我很困惑为什么在打印文件的最后一个字符后光标被带到新行?另外为什么总字符数(noc)24而不是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.

附:虽然我对“wc”显示的字符数有同样的问题,但你可以忽略“wc”outout,除了行数。我可能会发布下一个问题。

Thanks.

谢谢。

1 个解决方案

#1


2  

Why is the cursor taken to new line?

为什么光标被带到新线?

  • Because the '\n' character is in the file.
  • 因为'\ n'字符在文件中。

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

你也看到我的程序说字符数是24(与“wc poem.txt”输出内联)。

  • Because the 23 characters that were printed plus the '\n' are all in the file.
  • 因为打印的23个字符加上'\ n'都在文件中。

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.

注意:使用break是IMHO只在非常特殊的情况下才需要,我不喜欢它,因为它很难遵循程序流程。

#1


2  

Why is the cursor taken to new line?

为什么光标被带到新线?

  • Because the '\n' character is in the file.
  • 因为'\ n'字符在文件中。

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

你也看到我的程序说字符数是24(与“wc poem.txt”输出内联)。

  • Because the 23 characters that were printed plus the '\n' are all in the file.
  • 因为打印的23个字符加上'\ n'都在文件中。

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.

注意:使用break是IMHO只在非常特殊的情况下才需要,我不喜欢它,因为它很难遵循程序流程。