刘华世的官方博客

C语言文件操作 关于scanf遇空格结束

#include <stdio.h>

main()
{
FILE *fp;
char ch, st[2000];

    if((fp=fopen("c:/log.log","at++")) == NULL)
    {
        printf("Cannot open file strike any keys exit!");
        getch();
        exit(1);
    }

    printf("Input a string:\n");
    //gets(st);
    scanf("%[^\n]", st);
    fputs(st, fp);
    fputs("\n",fp);
    rewind(fp);
    ch = fgetc(fp);
    while(ch!=EOF)
    {
        putchar(ch);
        ch=fgetc(fp);
    }
    printf("\n");
    fclose(fp);
    getch();
}

scanf函数默认是遇空格便停止继续读字符了,要想让scanf函数继续读可以这样操作:

scanf("%[^\n]", name);

gets(name);

http://pythonschool.com/python/4.html 转摘

posted @ 2012-10-18 10:01  pythonschool  阅读(2303)  评论(0编辑  收藏  举报
刘华世的官方博客