IO第二天

用fgets函数计算文件行数

#include
#include
#include
#include

int main(int argc, const char *argv[])
{
	FILE* fp=fopen("./1.txt","r");
	if(NULL == fp)
	{
		ERR_MSG("fopen");
		return -1;
	}	
     
	char buf[20]="";
	int  count = 0;
	int  line=0;
	while(1)
	{
		if(fgets(buf,sizeof(buf),fp) == NULL)
			break;
        count+=strlen(buf);
		line++;
	}
	printf("count = %d,line = %d \n",count,line);

	fclose(fp);
    
	return 0;
}

用fwrite函数和fread函数实现拷贝

#include
#include
#include
#include

typedef struct
{
	char b[10];
	int a;
}INT;

int main(int argc, const char *argv[])
{
	FILE* fp_r=fopen("./3.txt","r");
	if(NULL == fp_r)
	{
		ERR_MSG("fopen");
		return -1;
	}
    FILE* fp_w=fopen("./5.txt","w+");
	if(NULL  == fp_w)
	{
		ERR_MSG("fopen");
		return -1;
	}
    INT t;
	size_t res =0;
	while(1)
	{
       memset(&t,0,sizeof(t));
	   res = fread(&t,sizeof(INT),1,fp_r);
	   if(0 == res)
		   break;
	   fwrite(&t,sizeof(INT),1,fp_w);
	}



	fclose(fp_r);
	fclose(fp_w);
	return 0;
}

IO第二天_第1张图片

 

你可能感兴趣的:(c++,c语言,算法)