C 无法正常工作以写入文件
我做了一个下面的程序。这个程序没有显示编译错误。目录中存在一个文件alive.txt。我想使用gets将字符串写入文件alive.txt,但它不能正常工作C 无法正常工作以写入文件,c,file-io,C,File Io,我做了一个下面的程序。这个程序没有显示编译错误。目录中存在一个文件alive.txt。我想使用gets将字符串写入文件alive.txt,但它不能正常工作 #include<stdio.h> #include<conio.h> #include<ctype.h> int main() { FILE *myfile; char c,d; char e[100]; myfile=fopen("alive.txt","r");
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
int main()
{
FILE *myfile;
char c,d;
char e[100];
myfile=fopen("alive.txt","r");
if(!myfile)
{
puts("Disk Error");
return(1);
}
while((c=fgetc(myfile))!=EOF)
{
putchar(c);
}
puts("\n");
puts("Do ya want to overwrite this file:{Y/N}");//
d=toupper(getchar());
if(d!='Y')
{
puts("Ok.. See ya Later");
return(1);
}
myfile=fopen("alive.txt","w");
puts("Type The Character to overwrite");
gets(e); // scanf works
fprintf(myfile,"%s",e);
fclose(myfile);
getch();
return(0);
}
#包括
#包括
#包括
int main()
{
文件*myfile;
c,d;
chare[100];
myfile=fopen(“alive.txt”、“r”);
如果(!myfile)
{
puts(“磁盘错误”);
申报表(1);
}
而((c=fgetc(myfile))!=EOF)
{
普查尔(c);
}
卖出(“\n”);
puts(“是否要覆盖此文件:{Y/N}”);//
d=toupper(getchar());
如果(d!=“Y”)
{
放置(“好的……待会儿见”);
申报表(1);
}
myfile=fopen(“live.txt”,“w”);
puts(“键入要覆盖的字符”);
获取(e);//scanf有效
fprintf(我的文件,“%s”,e);
fclose(myfile);
getch();
返回(0);
}
当您打开文件进行写入时,该文件已打开进行读取。加
fclose(myfile);
在第二个
fopen
之前,当您打开该文件进行写入时,该文件已打开进行读取。加
fclose(myfile);
#include<stdio.h>
//#include<conio.h> Non-portable
#include<ctype.h>
int main()
{
FILE *myfile;
char c,d;
char e[100];
myfile=fopen("alive.txt","r");
if(!myfile)
{
puts("Disk Error");
return(1);
}
/* Show the current file content. */
while((c=fgetc(myfile))!=EOF)
{
putchar(c);
}
fclose(myFile); // Close the read-only file mode.
puts("\n");
puts("Do ya want to overwrite this file:{Y/N}");
d=toupper(getchar());
if(d!='Y')
{
puts("Ok.. See ya Later");
return(1);
}
/*Flush '\n' character from stdin */
getchar();
puts("Type The Characters used to overwrite");
gets(e); // scanf works
myfile=fopen("alive.txt","w"); Re-open in write mode, deleting old file content.
fprintf(myfile,"%s",e);
fclose(myfile);
// getch(); Non-portable
/* Be nice and close the file when finished with it. */
fclose(myfile);
return(0);
}
在第二个
fopen
之前,当您打开该文件进行写入时,该文件已打开进行读取。加
fclose(myfile);
#include<stdio.h>
//#include<conio.h> Non-portable
#include<ctype.h>
int main()
{
FILE *myfile;
char c,d;
char e[100];
myfile=fopen("alive.txt","r");
if(!myfile)
{
puts("Disk Error");
return(1);
}
/* Show the current file content. */
while((c=fgetc(myfile))!=EOF)
{
putchar(c);
}
fclose(myFile); // Close the read-only file mode.
puts("\n");
puts("Do ya want to overwrite this file:{Y/N}");
d=toupper(getchar());
if(d!='Y')
{
puts("Ok.. See ya Later");
return(1);
}
/*Flush '\n' character from stdin */
getchar();
puts("Type The Characters used to overwrite");
gets(e); // scanf works
myfile=fopen("alive.txt","w"); Re-open in write mode, deleting old file content.
fprintf(myfile,"%s",e);
fclose(myfile);
// getch(); Non-portable
/* Be nice and close the file when finished with it. */
fclose(myfile);
return(0);
}
在第二个
fopen
之前,当您打开该文件进行写入时,该文件已打开进行读取。加
fclose(myfile);
#include<stdio.h>
//#include<conio.h> Non-portable
#include<ctype.h>
int main()
{
FILE *myfile;
char c,d;
char e[100];
myfile=fopen("alive.txt","r");
if(!myfile)
{
puts("Disk Error");
return(1);
}
/* Show the current file content. */
while((c=fgetc(myfile))!=EOF)
{
putchar(c);
}
fclose(myFile); // Close the read-only file mode.
puts("\n");
puts("Do ya want to overwrite this file:{Y/N}");
d=toupper(getchar());
if(d!='Y')
{
puts("Ok.. See ya Later");
return(1);
}
/*Flush '\n' character from stdin */
getchar();
puts("Type The Characters used to overwrite");
gets(e); // scanf works
myfile=fopen("alive.txt","w"); Re-open in write mode, deleting old file content.
fprintf(myfile,"%s",e);
fclose(myfile);
// getch(); Non-portable
/* Be nice and close the file when finished with it. */
fclose(myfile);
return(0);
}
在第二个fopen
#包括
#include<stdio.h>
//#include<conio.h> Non-portable
#include<ctype.h>
int main()
{
FILE *myfile;
char c,d;
char e[100];
myfile=fopen("alive.txt","r");
if(!myfile)
{
puts("Disk Error");
return(1);
}
/* Show the current file content. */
while((c=fgetc(myfile))!=EOF)
{
putchar(c);
}
fclose(myFile); // Close the read-only file mode.
puts("\n");
puts("Do ya want to overwrite this file:{Y/N}");
d=toupper(getchar());
if(d!='Y')
{
puts("Ok.. See ya Later");
return(1);
}
/*Flush '\n' character from stdin */
getchar();
puts("Type The Characters used to overwrite");
gets(e); // scanf works
myfile=fopen("alive.txt","w"); Re-open in write mode, deleting old file content.
fprintf(myfile,"%s",e);
fclose(myfile);
// getch(); Non-portable
/* Be nice and close the file when finished with it. */
fclose(myfile);
return(0);
}
//#包括非便携式
#包括
int main()
{
文件*myfile;
c,d;
chare[100];
myfile=fopen(“alive.txt”、“r”);
如果(!myfile)
{
puts(“磁盘错误”);
申报表(1);
}
/*显示当前文件内容*/
而((c=fgetc(myfile))!=EOF)
{
普查尔(c);
}
fclose(myFile);//关闭只读文件模式。
卖出(“\n”);
puts(“是否要覆盖此文件:{Y/N}”);
d=toupper(getchar());
如果(d!=“Y”)
{
放置(“好的……待会儿见”);
申报表(1);
}
/*从标准输入中刷新'\n'字符*/
getchar();
puts(“键入用于覆盖的字符”);
获取(e);//scanf有效
myfile=fopen(“alive.txt”,“w”);在写入模式下重新打开,删除旧文件内容。
fprintf(我的文件,“%s”,e);
fclose(myfile);
//getch();不可移植
/*做好准备,完成后关闭文件*/
fclose(myfile);
返回(0);
}
#包括
//#包括非便携式
#包括
int main()
{
文件*myfile;
c,d;
chare[100];
myfile=fopen(“alive.txt”、“r”);
如果(!myfile)
{
puts(“磁盘错误”);
申报表(1);
}
/*显示当前文件内容*/
而((c=fgetc(myfile))!=EOF)
{
普查尔(c);
}
fclose(myFile);//关闭只读文件模式。
卖出(“\n”);
puts(“是否要覆盖此文件:{Y/N}”);
d=toupper(getchar());
如果(d!=“Y”)
{
放置(“好的……待会儿见”);
申报表(1);
}
/*从标准输入中刷新'\n'字符*/
getchar();
puts(“键入用于覆盖的字符”);
获取(e);//scanf有效
myfile=fopen(“alive.txt”,“w”);在写入模式下重新打开,删除旧文件内容。
fprintf(我的文件,“%s”,e);
fclose(myfile);
//getch();不可移植
/*做好准备,完成后关闭文件*/
fclose(myfile);
返回(0);
}
#包括
//#包括非便携式
#包括
int main()
{
文件*myfile;
c,d;
chare[100];
myfile=fopen(“alive.txt”、“r”);
如果(!myfile)
{
puts(“磁盘错误”);
申报表(1);
}
/*显示当前文件内容*/
而((c=fgetc(myfile))!=EOF)
{
普查尔(c);
}
fclose(myFile);//关闭只读文件模式。
卖出(“\n”);
puts(“是否要覆盖此文件:{Y/N}”);
d=toupper(getchar());
如果(d!=“Y”)
{
放置(“好的……待会儿见”);
申报表(1);
}
/*从标准输入中刷新'\n'字符*/
getchar();
puts(“键入用于覆盖的字符”);
获取(e);//scanf有效
myfile=fopen(“alive.txt”,“w”);在写入模式下重新打开,删除旧文件内容。
fprintf(我的文件,“%s”,e);
fclose(myfile);
//getch();不可移植
/*做好准备,完成后关闭文件*/
fclose(myfile);
返回(0);
}
#包括
//#包括非便携式
#包括
int main()
{
文件*myfile;
c,d;
chare[100];
myfile=fopen(“alive.txt”、“r”);
如果(!myfile)
{
puts(“磁盘错误”);
申报表(1);
}
/*显示当前文件内容*/
而((c=fgetc(myfile))!=EOF)
{
普查尔(c);
}
fclose(myFile);//关闭只读文件模式。
卖出(“\n”);
puts(“是否要覆盖此文件:{Y/N}”);
d=toupper(getchar());
如果(d!=“Y”)
{
放置(“好的……待会儿见”);
申报表(1);
}
/*从标准输入中刷新'\n'字符*/
getchar();
puts(“键入用于覆盖的字符”);
获取(e);//scanf有效
myfile=fopen(“alive.txt”,“w”);在写入模式下重新打开,删除旧文件内容。
fprintf(我的文件,“%s”,e);
fclose(myfile);
//getch();不可移植
/*做好准备,完成后关闭文件*/
fclose(myfile);
返回(0);
}
问题在于get()
读取Y
后面的换行符,实现“行尾”,并返回空字符串(因为它删除了换行符),因此文件中没有写入任何内容。如果使用scanf(“%s”,e)
而不是gets()
,%s
格式跳过前导空格(例如Y
后面的换行符)并读取下一个单词
其他问题包括:
- 您还泄漏了文件描述符(文件流),因为您没有从第一个
开始丢失fopen()
myFile
- 您使用的是
,正如注释中所述,这是一个非常糟糕的主意。(建议gets()