简体   繁体   English

C-fgets如何与多个电话一起使用

[英]C - How fgets works with mutiple calls

I have the following code: 我有以下代码:

int i = 0;
char ar[500];

while(i < 20)
{
    printf("Type a line: ");
    fgets(ar, 500, stdin);
    fprintf(fp,"%s", ar);
    i++;
}

Basically, i am trying to open a file, then take 20 lines of input from the user and store it in an array, then print it to the file. 基本上,我试图打开一个文件,然后从用户那里获取20行输入并将其存储在数组中,然后将其打印到文件中。 It works fine but i don't understand how fgets works in the while loop. 它工作正常,但我不明白fgets在while循环中如何工作。 As i understand, when i = 0, fgets stores the 1st line to ar[500] and fprintf prints it out to the file, when i = 1, fgets stores the 2nd line to ar[500], but now ar[500] has 2 lines, but why fprintf only prints the 2nd line to the file but not all the 2 lines and so on when i increment by 1. 据我了解,当i = 0时,fgets将第一行存储到ar [500]中,而fprintf将其输出到文件中,当i = 1时,fgets将第二行存储到ar [500]中,但是现在ar [500]有2行,但是为什么当我递增1时,为什么fprintf仅将第二行打印到文件,而不是所有2行,依此类推。

fgets always start populating the array at arr[0]. fgets总是开始在arr [0]处填充数组。 At every iteration, the previous line is overwritten. 在每次迭代时,前一行将被覆盖。 fgets will add the null terminating character for you in arr, so only the currently read line will be outputed to FILE pointed to by fp. fgets将在arr中为您添加空终止符,因此仅当前读取的行将输出到fp指向的FILE中。

The description of char *fgets(char * restrict s, int n, FILE * restrict stream); char *fgets(char * restrict s, int n, FILE * restrict stream); is (C11 7.21.7p2) : (C11 7.21.7p2)

2 The fgets function reads at most one less than the number of characters specified by n from the stream pointed to by stream into the array pointed to by s . 2 fgets函数从stream指向的流到s指向的数组中读取的字符数最多少于n指定的字符数。 No additional characters are read after a new-line character (which is retained) or after end-of-file. 在换行符(保留)或文件结束之后,不会读取其他字符。 A null character is written immediately after the last character read into the array. 在将最后一个字符读入数组后,立即写入空字符。

The first character read by fgets will be always stored at s[0] and so forth up until at most s[n - 1] or the first newline character encountered (it being the last character stored). fgets读取的第一个字符将始终存储在s[0] ,依此类推,直到最多s[n - 1]或遇到的第一个换行符(它是最后存储的字符)。 The element after the last character read will be set to '\\0' , therefore terminating the string. 读取的最后一个字符之后的元素将设置为'\\0' ,从而终止字符串。

Ie effectively a fgets call will overwrite k + 1 first elements in the array where k is either the length of the line or n , whichever is smaller, and 1 means the null termination. 即有效地fgets调用将覆盖数组中的k + 1第一个元素,其中k为行的长度或n ,取较小者, 1表示空终止。

To store 20 lines, you'd need an array of arrays: 要存储20行,您需要一个数组数组:

char ar[20][500];

then read into ar[i] . 然后读入ar[i]

The buffer ar will be overwritten each time. 每次都将覆盖缓冲区ar

fgets has the prototype char *fgets(char *str, int n, FILE *stream) . fgets具有原型char *fgets(char *str, int n, FILE *stream) It will read a maximum of n-1 characters from stream and write them to str and then adds the '\\0' character. 它将从stream读取最多n-1字符并将它们写入str ,然后添加'\\0'字符。

regarding: "but now ar[500] has 2 lines," 关于:“但是ar [500]现在有2行,”

NO, the function fgets() does NOT append. 不,函数fgets()不追加。

Rather, it overlays. 而是覆盖。

In the posted code, always starting at ar[0] 在发布的代码中,始终从ar[0]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM