C语言循环写入文件,指定字符退出

#include <stdio.h>
#include <string.h>

int main()
{
    printf("%ld\n",strlen("quit"));
    FILE *fp=NULL;
    char file[200];
    char cc='\n';
    fp=fopen("writefile.txt","a+");
    if (fp==NULL) {
        printf("Open file failed\n");
    }else
    {
        printf("Open file successfully\n"); 
        while(1){
            scanf("%s",file);     
            fputs(file,fp);
            printf("--->>>%s %ld %ld\n",file,sizeof(file),strlen(file));
            fputc(cc,fp);
            int m;
            m=strcmp(file,"quit");
            printf("Is equal %d\n",m);
            if (m == 0) {
                printf("equal\n");
                break;
            }else
            {
                printf("not equal\n");
            }
            fflush(fp);
        } 
    }     
    return 0; 
}
C_project$ gcc myfile.c  -o a
C_project$ ./a
4
Open file successfully
hello
--->>>hello 200 5
Is equal -9
not equal
world
--->>>world 200 5
Is equal 6
not equal
hello world
--->>>hello 200 5
Is equal -9
not equal
--->>>world 200 5
Is equal 6
not equal
quit
--->>>quit 200 4
Is equal 0
equal
hello
world
hello
world
quit

 

posted @ 2020-06-22 01:14  腹肌猿  阅读(896)  评论(0编辑  收藏  举报