简体   繁体   English

为什么我的输出格式化为fgets中的“ \\ n”自动格式?

[英]Why is my output formatted, i.e. '\n' automatically in fgets?

Here is my code 这是我的代码

#include<stdio.h>
int main()
{
FILE* fp;
int i;
fp=fopen("newfile","r");
if(fp==NULL)
{
   printf("hhaha");
   return 0;
}
char str[20];
for(i=0;i<2;i++)
{
    fgets(str,20,fp);
    printf("%s",str);
} 
return 0;
}

Now if my newfile has text 现在,如果我的新文件中有文字

my name 我的名字
is xyz 是xyz

then how come when i print the two lines are printed in two newlines? 那当我打印两行并换成两行时又会怎样呢? where does the newline character come from? 换行符从何而来?

fgets sets the pointer to a char * representing the line of the file including the \\n at the end of the line. fgets将指针设置为char *的指针,该char *表示文件的行,在该行的末尾包含\\n (As is the case with most strings, it will also be '\\0' terminated) (与大多数字符串一样,它也将以'\\0'终止)

A file with this: 带有以下内容的文件:

This 这个
is
my 我的
file 文件

Will have this from fgets : 将来自fgets

This\\n\\0 , is\\n\\0 , my\\n\\0 , file\\n\\0 1 This\\n\\0is\\n\\0my\\n\\0file\\n\\0 1


1 The final value may not be include \\n . 1最终值不能包含\\n That will depend on whether it is a \\n terminated file. 这将取决于它是否为\\n终止文件。

from man fgets 来自man fgets

gets() reads a line from stdin into the buffer pointed to by s until either a terminating newline or EOF, which it replaces with '\\0'. gets()从stdin读取一行到s所指向的缓冲区,直到终止换行符或EOF,然后将其替换为'\\ 0'。 No check for buffer overrun is performed (see BUGS below). 不检查缓冲区溢出(请参阅下面的错误)。

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. fgets()最多从流中读取小于大小的字符,并将它们存储到s所指向的缓冲区中。 Reading stops after an EOF or a newline. 在EOF或换行符之后停止读取。 If a newline is read, it is stored into the buffer . 如果读取了换行符,则将其存储在buffer中 A '\\0' is stored after the last character in the buffer. 缓冲区的最后一个字符之后将存储一个'\\ 0'。

and thus fgets behaviour is different from what you might expect 因此fgets行为与您预期的不同

From the linux man page for fgets(): 在Linux手册页中的fgets()中:

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. fgets()最多从流中读取小于大小的字符,并将它们存储到s所指向的缓冲区中。 Reading stops after an EOF or a newline. 在EOF或换行符之后停止读取。 If a newline is read, it is stored into the buffer. 如果读取换行符,则将其存储到缓冲区中。 A '\\0' is stored after the last character in thebuffer. 缓冲区中的最后一个字符之后将存储一个'\\ 0'。

fgets() includes the newline when reading into the string - that's how fgets() is defined to work. 当读取字符串时, fgets()包括换行符-这就是fgets()定义为工作方式的方式。 From the standard: 从标准:

No additional characters are read after a new-line character (which is retained) or after end-of-file. 在换行符(保留)或文件结束之后,不会读取其他字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么名称字符串 ie gets 在我的程序中只执行一次? - why name string i.e. gets is executed only once in my program? 为什么我不能使用“fgets”将字符串读取到我的 Struct 元素中? - Why I cannot use "fgets" to read a string to an element of my Struct? 在C的数值输出中添加逗号? (即,输出为$ 12345.67,需要为12,345.67) - Add commas into numerical output in C? (i.e., output is $12345.67, need it to be 12,345.67) C为什么在单词后用\\ n书写时,我的fgets()无法正确读取行 - C Why can't my fgets() read line properly when writing with \n after words 什么时候系列之和 1+1/2+1/3+1/4+......+1/n=log n 什么时候系列之和等于n,即1+1/2+ 1/3+1/4+......+1/n=n - When is the sum of series 1+1/2+1/3+1/4+......+1/n=log n and when is the same sum equal to n, i.e. 1+1/2+1/3+1/4+......+1/n=n 使用Xcode 5调试时如何查看输出控制台(IE stdout)? - How to see the output console (I.E. stdout) when debugging with Xcode 5? 为什么只在最后一个循环点输出学生的信息? 即第19个学生 - Why is it only outputting the information for the student in the last loop spot? i.e. The 19th student 为什么putchar打印格式输出? - Why is putchar printing formatted output? 为什么在 c 中实现链表时使用动态内存分配(即 malloc())? - Why use dynamic memory allocation(i.e. malloc()) when implementing linked list in c? 结构的初始化函数(即“构造函数”),为什么变量未正确初始化? - Initialization function (i.e. “constructor”) for struct, why are variables not being initialized properly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM