这个程序是哪里错了吗 运行不了

这个程序是哪里错了吗 运行不了

问题描述:

#include <stdio.h>

main()

{ FILE *fp;

   char st [81];

   int i;

   if ((fp=fopen("text.txt","w+"))==NULL)

   { printf("Cannot open the file!\n"); exit(0);}

   for (i=0;i<10;i++)

{ gets(st);

   fputs(st,fp); fputs("\n",fp)

   }

 rewind(fp):

 printf("results:\n");

 for(i-0;i<10;i++)

{ fgets(st,81,fp);

 printf("%s",st);

 fclose(fp); }

 system("pause");

}

修改如下,供参考:

#include <stdio.h>

main()

{ FILE *fp;

   char st [81];

   int i;

   if ((fp=fopen("text.txt","w+"))==NULL)

   {
       printf("Cannot open the file!\n");
       exit(0);
   }

   for (i=0;i<10;i++)

    {
       gets(st);

       fputs(st,fp);

       fputs("\n",fp);//缺了 ‘;’
    }

    rewind(fp);//:

    printf("results:\n");

    for(i=0;i<10;i++)//for(i-0;i<10;i++)

    {
       fgets(st,81,fp);

       printf("%s",st);

    }
    fclose(fp);    //关闭文件移到循环外
    
    system("pause");

}

 

void main()

{
	FILE *fp;

	char st[81];

	int i;

	if ((fp = fopen("text.txt", "w+")) == NULL)

	{
		printf("Cannot open the file!\n"); exit(0);
	}

	for (i = 0; i < 10; i++)

	{
		gets(st);

		fputs(st, fp);
		fputs("\n", fp);

	}

	rewind(fp);

		printf("results:\n");

	for (i - 0; i < 10; i++)

	{
		fgets(st, 81, fp);

		printf("%s", st);

		fclose(fp);
	}

	system("pause");

}

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    FILE *fp;
    char st[81];
    int i;

    if ((fp=fopen("text.txt","w+"))==NULL) {
        printf("Cannot open the file!\n");
        exit(0);
    }

    for (i=0;i<10;i++) {
        fgets(st, 81, stdin);

        fputs(st,fp);
        //fputs("\n",fp);
    }

    rewind(fp);

    printf("results:\n");

    //for(i-0;i<10;i++)
    for (i = 0; i < 10; i++) {
        fgets(st,81,fp);
        printf("%s",st);
        //fclose(fp);
    }
    fclose(fp);

    system("pause");

    return 0;
}

供参考~

 

文件关闭不能放在循环里,否则提前打开文件,再次读取文件就会有问题的

其他问题供参考~