首页 > 编程语言 > 详细

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

时间:2020-06-22 09:19:53      阅读:81      评论:0      收藏:0      [点我收藏+]
#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

 

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

原文:https://www.cnblogs.com/pfeiliu/p/13174972.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!