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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM