feof

how use EOF stdin in C

喜欢而已 提交于 2019-12-05 01:14:42
问题 I need to input coordinates into an array until EOF is encountered, but something is wrong in my code. I used 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; } 回答1: I suggest using while(!feof(stdin) && scanf ( "%d %d ", &a, &b) == 2) and actually it is better to test

feof() and fscanf() stop working after scanning byte 1b as a char. Is it because it is 'ESC' in ascii? What can I do?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 06:38:25
问题 I'm currently writing a program that processes PPM files (P6 type, not P3) The problem is that some images have the byte 0x1b which, according to the ascii table is known as 'ESC' The following is pretty much my code: // all the includes there, , , ... 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);

How feof() works in C

ぐ巨炮叔叔 提交于 2019-12-03 17:57:37
问题 Does feof() checks for eof for the current position of filepointer or checks for the position next to current filepointer? Thanks for your help ! 回答1: Every FILE stream has an internal flag that indicates whether the caller has tried to read past the end of the file already. feof returns that flag. The flag does not indicate whether the current file position is as the end of the file, only whether a previous read has tried to read past the end of the file. As an example, let's walk through

how use EOF stdin in C

大憨熊 提交于 2019-12-03 16:00:53
I need to input coordinates into an array until EOF is encountered, but something is wrong in my code. I used 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; } I suggest using while(!feof(stdin) && scanf ( "%d %d ", &a, &b) == 2) and actually it is better to test feof after (not before!) some input operation, so: while (scanf("%d %d ", &a, &b) == 2 && !feof(stdin)) BTW,

warning feof() expects parameter 1 to be resource

匿名 (未验证) 提交于 2019-12-03 01:58:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: My error logs are getting out of control with the two below errors warning feof() expects parameter 1 to be resource and warning fread() expects parameter 1 to be resource The bit of code responsible is

warning feof() expects parameter 1 to be resource

匿名 (未验证) 提交于 2019-12-03 01:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: My error logs are getting out of control with the two below errors warning feof() expects parameter 1 to be resource and warning fread() expects parameter 1 to be resource The bit of code responsible is

cin.eof() and feof(stdin) are discordant

匿名 (未验证) 提交于 2019-12-03 00:59:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Can feof(stdin) and cin.eof() produce discordant results? I would expect them not to do so, but I guess I am missing something. Does the standard say anything about this? Consider the following example, when no input is given. This can either be done by typing the shortcut for EOF in the terminal, or by redirecting the input on /dev/null (Unix), or with empty stdin sequence on ideone.com ... #include <cstdio> #include <iostream> int main() { using namespace std; char ch; cin >> ch; cin.clear(); cout << boolalpha << "feof(stdin): " << bool

C语言文件操作函数之ferror &amp; feof &amp; clearerr

匿名 (未验证) 提交于 2019-12-03 00:43:02
这些函数都是和文件读写时发生错误有关,下面一一分析: 1:ferror 原型:int ferror(FILE * fp) 作用:测试一个文件流是否被设置了错误标识符,如果是返回非 0 整数,否则返回 0。 例子: short ch; FILE * f1; f1 = fopen( " test.txt " , " w " ); ch = fgetc(f1); // 注意,我们以 只写 方式打开了文件,但是却尝试向该文件读取一个字符,肯定会操作失败,这时,该文件流就会被设置上错误标识符 printf( " %d\n " , ch); // 这里输出 -1 ,因为当fgetc函数操作失败时返回 EOF,即 -1 if (ferror(f1)){ // 用函数ferror测试文件流是否存在错误标识符 printf( " Have a error.\n " ); // 结果自然是,所以输出错误信息 } fclose(f1); 注意:每次发生不同的操作错误都会更新文件流上次保存的错误标识符,例如:先后发生了A和B两个文件操作错误,并且A错误的标识符为16,B错误的标识符为32,那么文件流保存的错误标识符就是 32,用ferror(fp)读取返回的结果就是 32。 2:feof 原型:int feof(FILE * fp) 作用:测试一个文件流是否被设置了文件结束标识符,如果是返回非 0 整数

php中连接tcp服务的三种方式

匿名 (未验证) 提交于 2019-12-02 22:11:45
首先需要现有一个 tcp 服务,我们使用 php中的 socket 系列函数实现 <?php //创建socket套接字 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); //设置阻塞模式 socket_set_block($socket); //为套接字绑定ip和端口 socket_bind($socket,'127.0.0.1',3046); //监听socket socket_listen($socket,4); while(true) { //接收客户端请求 if(($msgsocket = socket_accept($socket)) !== false) { //读取请求内容 $buf = socket_read($msgsocket, 8192); echo "Received msg: $buf \n"; $str = "this is a service message"; //向连接的客户端发送数据 socket_write($msgsocket, $str,strlen($str)); //操作完之后需要关闭该连接否则 feof() 函数无法正确识别打开的句柄是否读取完成 socket_close($msgsocket); } } 连接 tcp 服务: 使用 socket 系列函数连接 <?php

feof() and fscanf() stop working after scanning byte 1b as a char. Is it because it is 'ESC' in ascii? What can I do?

半腔热情 提交于 2019-12-02 12:08:29
I'm currently writing a program that processes PPM files (P6 type, not P3) The problem is that some images have the byte 0x1b which, according to the ascii table is known as 'ESC' The following is pretty much my code: // all the includes there, , , ... 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] = fscanf(finput, "%c", &g); num[2] = fscanf(finput, "%c