首页 > 解决方案 > 在c中读取和写入文本文件

问题描述

一个简单的程序,用于对文本文件执行写入操作,然后从文件中读取并显示文本。我该如何纠正它以及如何避免使用“#define _CRT_SECURE_NO_WARNINGS”我正在使用visual studios 2018我已经尝试了多次但输出似乎无限输出

   #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
   #include <stdlib.h>
    void main()
    {
        FILE *fp1, *fp2;
        fp1 = fopen("text.txt", "w");
        printf("enter no of lines");
        int line;
        scanf("%d", &line);

        char txt[1000];
        for (int i=0; i < line; i++)
        {
            gets( txt);
            fprintf(fp1, "s", txt);

        }
        fclose(fp1);
        char txt2[1000];
        fp2 = fopen("text.txt", "r");
        while (1)
        {
            fgets(txt2, 1000, fp2);

            if (txt2 == EOF)
                break;
            printf("%s", txt2);
        }
        fclose(fp2);
    }`

标签: cfileloopstexteof

解决方案


推荐阅读