none
程序死循環, 不斷重複最後一個字符, 求高手解答 RRS feed

  • 问题

  • #include <stdio.h>
    #include <process.h>
    void main()
    {
    	FILE *fp;
    	if ((fp = fopen("C:\\Documents and Settings\\Administrator\\桌面\\testA.txt","r"))!=NULL)
    	{
    		for (char a=NULL;a!=EOF;printf("%c",a))
    			fscanf(fp,"%c", &a);
    		fclose(fp);
    	}
    	else printf("opening error\n");
    }


    2012年8月6日 16:38

答案

  • 根据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");
        }
    }

    2012年8月6日 21:20
  • 应该是指“%”代表的内容,比如“%c"就是一个field,如果被扫描并赋值,则返回值为1.如果调用了fscanf(fp, ”%c%d%d", &ch, &num1, &num2),如果返回值 为3,说明所有的ch, num1和num2都被扫描到了并赋了值,如果返回值为2,则说明只有ch和num1被扫描到了并赋了值。以此类推。所以,用"%"表示的扫描项,就是这里面所说的“field".
    2012年8月7日 3:50

全部回复

  • 根据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");
        }
    }

    2012年8月6日 21:20
  • 根据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 指的是什麼, 謝謝~


    2012年8月7日 2:16
  • 应该是指“%”代表的内容,比如“%c"就是一个field,如果被扫描并赋值,则返回值为1.如果调用了fscanf(fp, ”%c%d%d", &ch, &num1, &num2),如果返回值 为3,说明所有的ch, num1和num2都被扫描到了并赋了值,如果返回值为2,则说明只有ch和num1被扫描到了并赋了值。以此类推。所以,用"%"表示的扫描项,就是这里面所说的“field".
    2012年8月7日 3:50