Tag: feof

如何在fgets中使用feof和ferror(C中的minishell)

我写过这个minishell,但我不确定我是否正确控制错误。 我知道fgets可以返回feof和ferror( http://www.manpagez.com/man/3/fgets/ ),但我不知道如何使用它们。 我已经检查过fgets是否返回一个空指针(表示缓冲区的内容是inditerminate),但我想知道如何使用feof和ferror。 #include #include #include #include #define LINE_LEN 50 #define MAX_PARTS 50 int main () { char* token; char str[LINE_LEN]; char* arr[MAX_PARTS]; int i,j; bool go_on = true; while (go_on == true){ printf(“Write a line:(‘quit’ to end) \n $:”); fgets(str, LINE_LEN, stdin); if (str==NULL) { goto errorfgets; } else { size_t l=strlen(str); […]

C如何处理EOF?

#include int main() { FILE* f=fopen(“book2.txt”,”r”); char a[200]; while(!feof(f)) { fscanf(f,”%s”,a); printf(“%s “,a); printf(“%d\n”,ftell(f)); } fclose(f); return 0; } 我有上面的代码。 book2.txt包含“abcdef abcdef”,光标移动到换行符(即:abcdef abcdef \ n)。 我得到以下结果。 abcdef 6 abcdef 13 abcdef 19 我希望得到 abcdef 6 abcdef 13 15 我究竟做错了什么?

使用feof()使用fscanf()

这是我的代码。 #include void main(){ FILE *fp; int a,b; fp=fopen(“hello.txt”,”r”); while(!feof(fp)){ fscanf(fp,”%d %d”,&a,&b); printf(“%d %d\n”,a,b); } } 我的hello.txt是 1 2 3 4 我的输出是 1 2 3 4 4 4 为什么我的最后一行被打印两次。 还没有fp达到EOF? 此外,stackoverflow中的标记说Usually, when it is used, the code using it is wrong. 这是什么意思? 谢谢。

如何在C中使用EOF stdin

我需要将坐标输入到数组中,直到遇到EOF,但我的代码中出现了错误。 我用ctrl + Z,ctrl + D. int main() { int x[1000],y[1000]; int n=0,nr=0,a,b,i; printf(“Enter the coordinates:\n”); while(scanf ( “%d %d “, &a, &b) == 2) { x[n]=a; y[n]=b; n++; } if (!feof(stdin)) { printf(“Wrong\n”); } else { for(i=0;i<n;i++) printf("%d %d\n", x[i], y[i]); } return 0; }

feof()和fscanf()在将字节1b扫描为char后停止工作。 是因为它在ascii中是’ESC’吗? 我能做什么?

我正在编写一个处理PPM文件的程序(P6类型,而不是P3)问题是一些图像的字节为0x1b,根据ascii表称为’ESC’ 以下几乎是我的代码: //所有包括那里,,, … int main(void) { FILE *finput; int number[7]; char r, g, b; finput = fopen(“my.ppm”, “r”); /* The code where I read the P6 numRows numColumns 255\n …Lets assume that part works well */ while(!feof(finput)) { num[0] = fscanf(finput, “%c”, &r); // the “num[] = ” part is for debugging num[1] = […]

如何通过只输入一个EOF来结束scanf

我在讨论这个问题。 我正在使用while循环来扫描数字串,需要结束扫描并开始继续我的程序的其余部分。 我只是想不通,如何刷新标准输入或做任何事情都不按两次Ctrl + D. 我只需要发送一次EOF来告诉我的循环结束。 while (! feof (stdin)) {status=scanf (“%d”, &array[i]); if ( (status != 1 && status != EOF) ) { printf(“\nWrong input.\n”); return 1;} i++;}

feof()在C中是如何工作的

feof()是否为filepointer的当前位置检查eof或检查当前filepointer旁边的位置? 谢谢你的帮助 !

为什么这个C代码有问题?

在另一个问题上 , Jerry Coffin指出了以下内容: 它(可能)与你的问题并不真正相关,但是while (!feof(fileptr)){几乎是一个有保障的bug。 我想我会开始一个单独的问题,因为这个评论有些偏离主题。 有人可以向我解释一下吗? 这是我以前用C编写的第一个程序。

如何使用feof(FILE * f)?

我在使用do-while循环时遇到困难,当我们到达文件末尾时应该停止。 这是循环代码: do { if (pcompanyRow[0] != ‘#’ && pass == 1) { strtok(pcompanyRow, “:”); pcompanyName = strcpy(pcompanyName, strtok(NULL, “”)); pass = 2; fgets(pcompanyRow, 1024, f); } if (pcompanyRow[0] != ‘#’ && pass == 2) { strtok(pcompanyRow, “:”); pcompanySMSPrice = strcpy(pcompanySMSPrice, strtok(NULL , “”)); pass = 3; fgets(pcompanyRow, 1024 , f); } if (pcompanyRow[0] != […]

为什么fread会提早到达EOF?

我正在编写一个将文件读入内存的C库。 它会跳过文件的前54个字节(标题),然后将余数作为数据读取。 我使用fseek来确定文件的长度,然后使用fread来读取文件。 循环运行一次然后结束,因为达到了EOF(没有错误)。 最后,bytesRead = 10624,ftell(stream)= 28726,缓冲区包含28726个值。 我希望fread读取30,000字节,当达到EOF时文件位置为30054。 C不是我的母语所以我怀疑我的某个地方有一个愚蠢的初学者错误。 代码如下: const size_t headerLen = 54; FILE * stream; errno_t ferrno = fopen_s( &stream, filename.c_str(), “r” ); if(ferrno!=0) { return -1; } fseek( stream, 0L, SEEK_END ); size_t bytesTotal = (size_t)(ftell( stream )) – headerLen; //number of data bytes to read size_t bytesRead = 0; […]