把a文件里面的内容全部给b文件里面去

代码如下

#include

int main(int argc, char const *argv[])
{
	FILE * fp = fopen("a.txt","r");
	if(fp == NULL)
	{
		perror("Error opening file");
		return 0;
	}
	FILE * fp2 = fopen("b.txt","a");
	if(fp2 == NULL)
	{
		perror("Error opening file");
		return 0;
	}
	char buf[8][28] = {""};
	int i = 0;
	//把数据全部读到二维数组里面
	while (1)
	{
		char *ret = fgets(buf[i++],sizeof(buf[i]),fp);
		if(ret == NULL)
		{
			break;
		}
	}
	//把二维数组里面的数据写到b.txt
	for(i = 0;i < 8;i++)
	{
		int a = fputs(buf[i],fp2);
		int b = fputs("\n",fp2);
		if (a == EOF || b == EOF)
		{
			printf("over\n");
			break;
		}
		
	}
	fclose(fp);
	fclose(fp2);
	return 0;
}

你可能感兴趣的:(小白抄代码日记,算法,linux,运维)