int main()
{
FILE *ft;
char ch;
ft=fopen("abc.txt","r+");
if(ft==NULL)
{
printf("can not open target file\n");
exit(1);
}
while(1)
{
ch=fgetc(ft);
if(ch==EOF)
{
printf("done");
break;
}
if(ch=='i')
{
fputc('a',ft);
}
}
fclose(ft);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
正如我可以看到的那样,我想以abc.txt一种i被其替换的方式进行编辑a.
该程序工作正常,但当我在abc.txt外部打开时,它似乎是未经编辑的.
任何可能的原因?
为什么在这种情况下,后面的字符i不会被替换a,如答案所示?
我读了很多关于的问题
declaration of functions inside structure?
但我没有得到满意的答案.
我的问题不在于此 whether functions can be declared inside a structure or not?
相反,我的问题是
WHY function can not be declared inside structure?
int main()
{
FILE *fs,*ft;
char ch;
fs=fopen("main.c","r");
if(fs==NULL)
{
printf("can not open source file\n");
getch();
exit(1);
}
ft=fopen("demo.docx","w");
if(ft==NULL)
{
printf("can not open target file\n");
getch();
exit(1);
}
while(1)
{
ch=fgetc(fs);
if(ch==EOF)
{
break;
}
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,我试图将内容复制main.c到demo.docx.
该程序工作正常,但当我外部打开时demo.docx,它崩溃了.
我身边有问题,或者我们无法docx使用c 写入文件?