feof

How does C handle EOF? [duplicate]

不想你离开。 提交于 2019-12-24 01:43:15
问题 This question already has answers here : Why is “while ( !feof (file) )” always wrong? (4 answers) Closed 6 years ago . #include <stdio.h> 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; } I have the code above. book2.txt contains "abcdef abcdef" with the cursor move to a newline(ie:abcdef abcdef\n). I get the results below. abcdef 6 abcdef 13 abcdef 19 I expect to get abcdef 6

How to use feof(FILE *f)?

那年仲夏 提交于 2019-12-17 16:53:39
问题 I'm having a hard time with a do-while loop, that is supposed to stop when we reach the end of the file. Here's the loop code: 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] != '#' &

Why is fread reaching the EOF early?

不羁岁月 提交于 2019-12-17 16:27:03
问题 I am writing a C library that reads a file into memory. It skips the first 54 bytes of the file (header) and then reads the remainder as data. I use fseek to determine the length of the file, and then use fread to read in the file. The loop runs once and then ends because the EOF is reached (no errors). At the end, bytesRead = 10624, ftell(stream) = 28726, and the buffer contains 28726 values. I expect fread to read 30,000 bytes and the file position to be 30054 when EOF is reached. C is not

linux socket 清空缓存区

前提是你 提交于 2019-12-14 21:35:19
情况一:知晓缓存区中数据的大小 这种情况应该就不用多说了,直接循环的把数据都读取出来就行了。 情况二:不知道缓存区中数据的大小 方案一 close一次socket,这个方案有效是有效,但这样的小问题还不至于如此大动干戈,因此不建议使用。 方案二 使用recv来读取,但是在阻塞模式下效率低下,因为在不知道数据的情况下,在最后一次读取的情况下需要等待到超时才会知道数据是否读取完毕。 方案三 使用fgetc,通过判断feof来确定数据时候读取完毕。 whlie (1) { a=fgetc(f); if (feof(f)) break; //… b=fgetc(f); if (feof(f)) break; //… } 方案四 使用select()来实现,这个的思想其实就是巧妙的将阻塞型转化成非阻塞型,而且不需要直接改动原来的socket。(注:需要了解select中超时时间三种情况 1) void cleanBuff(SOCKET sock_conn){ // 设置select立即返回 timeval time_out; time_out.tv_sec = 0; time_out.tv_usec = 0; // 设置select对sock_conn的读取感兴趣 fd_set read_fds; FD_ZEROS(&read_fds); FD_SET(sock_conn, &read

Why is “while ( !feof (file) )” always wrong?

為{幸葍}努か 提交于 2019-12-11 15:17:09
问题 I've seen people trying to read files like this in a lot of posts lately. Code #include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { char *path = argc > 1 ? argv[1] : "input.txt"; FILE *fp = fopen(path, "r"); if( fp == NULL ) { perror(path); return EXIT_FAILURE; } while( !feof(fp) ) { /* THIS IS WRONG */ /* Read and process data from file… */ } if( fclose(fp) == 0 ) { return EXIT_SUCCESS; } else { perror(path); return EXIT_FAILURE; } } What is wrong with this loop? 回答1: I'd

Problem with reading file…while (!feof(file)) leads to infinite loop!

会有一股神秘感。 提交于 2019-12-11 10:13:50
问题 void OpenFile() { FILE *fp; char buffer[1024]; int number; fp=fopen("godess.txt","r"); if(fp==NULL){ printf("Error opening file!\n"); exit(0); } else { while (!feof(fp)) { printf("In loop!!!!"); fscanf(fp,"%d\n",&number); } fclose(fp); } } Hello...I'm trying to read a file ...but in the line where I write " while (!feof(fp))"....it leads in an infinite loop....can anyone please help me to solve this matter..Please I have tried everything...but nothing happens!!! 回答1: You should check the

Using fscanf() using feof()

有些话、适合烂在心里 提交于 2019-12-10 18:37:44
问题 Here's my code. #include<stdio.h> 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); } } My hello.txt is 1 2 3 4 My Output is 1 2 3 4 4 4 Why is my last line being printed twice. Has not fp reached EOF? Also,the tag in stackoverflow says Usually, when it is used, the code using it is wrong. What does it mean? Thanks. 回答1: You must never perform an input operation without immediately checking its result! The following

雷林鹏分享:PHP 文件处理

廉价感情. 提交于 2019-12-10 02:30:45
  fopen() 函数用于在 PHP 中打开文件。   打开文件   fopen() 函数用于在 PHP 中打开文件。   此函数的第一个参数含有要打开的文件的名称,第二个参数规定了使用哪种模式来打开文件:            $file=fopen("welcome.txt","r");   ?>         文件可能通过下列模式来打开:   模式描述   r只读。在文件的开头开始。   r+读/写。在文件的开头开始。   w只写。打开并清空文件的内容;如果文件不存在,则创建新文件。   w+读/写。打开并清空文件的内容;如果文件不存在,则创建新文件。   a追加。打开并向文件末尾进行写操作,如果文件不存在,则创建新文件。   a+读/追加。通过向文件末尾写内容,来保持文件内容。   x只写。创建新文件。如果文件已存在,则返回 FALSE 和一个错误。   x+读/写。创建新文件。如果文件已存在,则返回 FALSE 和一个错误。   注释:如果 fopen() 函数无法打开指定文件,则返回 0 (false)。   实例   如果 fopen() 函数不能打开指定的文件,下面的实例会生成一段消息:            $file=fopen("welcome.txt","r") or exit("Unable to open file!");   ?>        

Paypal IPN Script, issue with feof and fgets

喜你入骨 提交于 2019-12-06 16:28:19
问题 I've been having issues with my Paypal IPN listener script for a couple of days now. For those of you who are unfamiliar with the Paypal IPN system, basically Paypal sends your script with a message about the transaction, which you send back with a couple of bits added. If Paypal receives the correct reply, it'll reply with 'VERIFIED', and if not it'll say 'INVALID'. I initially thought that the problem I was experiencing was with the 'fsockopen' command: $fp=fsockopen('ssl://sandbox.paypal

Reading data from fsockopen using fgets/fread hangs

一曲冷凌霜 提交于 2019-12-05 20:54:22
问题 Here is the code that I am using: if (!($fp = fsockopen('ssl://imap.gmail.com', '993', $errno, $errstr, 15))) echo "Could not connect to host"; $server_response = fread($fp, 256); echo $server_response; fwrite($fp, "C01 CAPABILITY"."\r\n"); while (!feof($fp)) { echo fgets($fp, 256); } I get the first response: OK Gimap ready for requests from xx.xx.xx.xx v3if9968808ibd.15 but then the page times out. I have searched through stream_set_blocking, stream_set_timeout, stream_select, fread, etc.