积极答复者
程序死循環, 不斷重複最後一個字符, 求高手解答

问题
答案
-
根据MSDN:http://msdn.microsoft.com/en-us/library/cb5kac8b.aspx
Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. If an error occurs, or if the end of the file stream is reached before the first conversion, the return value is EOF for fscanf and fwscanf.
EOF是fscanf的返回值,不是a的值。所以,代码就改为:
for (char a=NULL;fscanf(fp, "%c", &a) != EOF;printf("%c",a));
另:此代码的编码风格问题很大,通常的工程中是不会这样写的,会带来很多问题,如理解困难及难以维护。建议修改如下:
#include <stdio.h> #include <process.h> void main() { FILE *fp = fopen("C:\\Documents and Settings\\Administrator\\桌面\\testA.txt","r"); if (fp != NULL) { char a = '\0'; while (fscanf(fp, "%c", &a) != EOF) { printf("%c", a); } fclose(fp); } else { printf("opening error\n"); } }
全部回复
-
根据MSDN:http://msdn.microsoft.com/en-us/library/cb5kac8b.aspx
Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. If an error occurs, or if the end of the file stream is reached before the first conversion, the return value is EOF for fscanf and fwscanf.
EOF是fscanf的返回值,不是a的值。所以,代码就改为:
for (char a=NULL;fscanf(fp, "%c", &a) != EOF;printf("%c",a));
另:此代码的编码风格问题很大,通常的工程中是不会这样写的,会带来很多问题,如理解困难及难以维护。建议修改如下:
#include <stdio.h> #include <process.h> void main() { FILE *fp = fopen("C:\\Documents and Settings\\Administrator\\桌面\\testA.txt","r"); if (fp != NULL) { char a = '\0'; while (fscanf(fp, "%c", &a) != EOF) { printf("%c", a); } fclose(fp); } else { printf("opening error\n"); } }
-
根据MSDN:http://msdn.microsoft.com/en-us/library/cb5kac8b.aspx
Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. If an error occurs, or if the end of the file stream is reached before the first conversion, the return value is EOF for fscanf and fwscanf.
EOF是fscanf的返回值,不是a的值。所以,代码就改为:
for (char a=NULL;fscanf(fp, "%c", &a) != EOF;printf("%c",a));
另:此代码的编码风格问题很大,通常的工程中是不会这样写的,会带来很多问题,如理解困难及难以维护。建议修改如下:
#include <stdio.h> #include <process.h> void main() { FILE *fp = fopen("C:\\Documents and Settings\\Administrator\\桌面\\testA.txt","r"); if (fp != NULL) { char a = '\0'; while (fscanf(fp, "%c", &a) != EOF) { printf("%c", a); } fclose(fp); } else { printf("opening error\n"); } }
還有問題:
Each of these functions returns the number of fields successfully converted and assigned;
這一句裡面的 fields 指的是什麼, 謝謝~