问题4 文件的相关问题

1,文件的复制

#include <stdio.h>
#include <stdlib.h> 
int main()
{
    
    
	FILE *in, *out;
	char ch, infile[30], outfile[30];
	scanf("%s%s", infile, outfile);//输入文件名 
	if ( (in = fopen(infile, "r")) == NULL)
	{
    
    
		printf("infile error!\n");
		exit(0);
	}
	if ( (out = fopen(outfile, "w")) == NULL)
	{
    
    
		printf("outfile error!\n");
		exit(0);
	}
	while ( !feof(in) )//文件未读完时 
	               //feof函数值为1,表示文件读取结束;函数值为0,表示文件未读取结束 
	{
    
    
		ch = fgetc(in);//从in文件读取一个字符,赋给ch
		fputc(ch, out);//将该字符写到out文件中 
		putchar(ch);
	}//循环结束,in文件里的所有字符都被写到out文件中,即完成复制 
	fclose(in);
	fclose(out);
	return 0;
} 

2,将文件中字符串存到数组中

int m = 0;
while ( !feof(a) )
		ch[m ++] = fgetc(a);

猜你喜欢

转载自blog.csdn.net/Shao_yihao/article/details/113485502