我做了个密码:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char * argv[]) // taking files
{
FILE * fp;
FILE * fp2;
int c;
.
. // checking possible problems
.
while ( (c = fgetc(fp)) != EOF){ // copying one file to another one
fputc (c, stdout);
fputc (c, fp2);
}
fclose(fp);
fclose(fp2);
fp = NULL;
fp2 = NULL;
return EXIT_SUCCESS;
}我想问您,是否可以使用fgets函数而不是类似于fgetc的函数来做相同的while语句:
fgets(fp, sizeof(fp), stdin));发布于 2021-11-09 22:12:09
是的,可以使用fget()和fput()编写这个程序。我们知道fget()在遇到换行符或EOF后停止读取,我们可以使用此属性,但在本例中,我们不能直接使用feof()函数,否则,我们可以使用两个文件指针并执行如下操作:
//打开要从"r“模式读取的file<*fp>和以"w”模式写入的file<*fp1>之后。
while(1){
if(fgetc(fp)!=EOF) fseek(fp,-1,1);
else break;
fgets(buff,100,fp);
fputs(buff,fp1);}https://stackoverflow.com/questions/53402455
复制相似问题