qq_53677326
秘密之狐
2021-01-17 17:00

这两个一样的代码对同一个文件咋一个打的开一个打不开嘞

  • c++

这是网上大大的代码

#include <stdio.h>
int main()
{
	FILE *fp;
    char ch,filename[20];
	printf("Please input your filename:");
	scanf("%s",filename);
    if(!(fp=fopen(filename,"w")))
	{
		printf("Can not open %s\n",filename);
	}
	else
	{
		printf("Please input the sentences you write:");
		ch=getchar();
		ch=getchar();
        while(ch!=EOF)
		{
			fputc(ch,fp);
			ch=getchar();
		}
		fclose(fp);
	}
	    if(!(fp=fopen(filename,"r")))
	{
		printf("Can not open %s\n",filename);
	}
	else
	{
		printf("The content of %s is:",filename);
        while(!feof(fp))
		{
			ch=fgetc(fp);
			putchar(ch);
		}
		printf("\n");
		fclose(fp);
	}
	return 0;
}

这是萌新的我的代码

#include<stdio.h>
#include<stdlib.h>
int main ()
{
	FILE *fp;
	char ch,filename[20];
	printf("please input the filename you want to write:");
	scanf("%s",filename);
	
	if(!(fp=fopen(filename,"w")));
	{printf("can not open the file");
	exit(0);
    }
    printf("please input the sentences you want:");
    ch=getchar();
    ch=getchar();
    while(ch!=EOF)
    {
    	fputc(ch,fp);
    	ch=getchar();
	}
	fclose(fp);
	}

感觉明明是一样的啊

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答