20230726

 用fread和fwrite实现文件拷贝

#include 

int main()
{
	char s[128] = "";
	char *ps = s;
	FILE *fp = fopen("txt1.c","r");
	if(NULL == fp)
	{
		perror("fopen");
		return -1;
	}

	int num1=0;
	num1 = fread(ps,sizeof(s),1,fp);
	printf("num1 = %d\n",num1);

//	printf("%c",*ps);
//	printf("移至SET\n");
	fseek(fp,0,SEEK_SET);

	int num=0;
	FILE *fp1 = fopen("txt2.c","w+");
	num = fwrite(ps,sizeof(s),1,fp1);
	printf("num = %d\n",num);

	fclose(fp);
	return 0;
}

使用fgets实现计算一个文件有几行

#include "head.h"
#define MAX 1024
int main()
{
	char s1[MAX];
	int count = 0;
	FILE *fp = fopen("txt1.c","r");
	if(NULL == fp)
	{
		perror("fopen");
		return -1;
	}
	while(1)
	{
		if(NULL == fgets(s1,MAX,fp))
		{
			break;
		}
		count++;
	}
	printf("行数为:%d\n",count);

	return 0;
}

思维导图20230726_第1张图片

 

你可能感兴趣的:(java,算法,开发语言)