快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

C语言文件操作函数大全-创新互联

fgetc_fputc

成都服务器托管,成都创新互联公司提供包括服务器租用、成都天府联通服务器托管、带宽租用、云主机、机柜租用、主机租用托管、CDN网站加速、申请域名等业务的一体化完整服务。电话咨询:18982081108
#include#includeint main()
{FILE *fp_in;
	FILE *fp_out;
	char c;
	if((fp_in=fopen("D:\\cProgram\\data_in.txt","w+"))==NULL)
	{printf("open file error!\n");
		exit(0);
	}
	c=getchar();
	while(c!='#')
	{fputc(c,fp_in);
		c=getchar();
	}
	fclose(fp_in);
	if((fp_in=fopen("D:\\cProgram\\data_in.txt","r"))==NULL)
	{printf("open file error!\n");
		exit(0);
	}
	if((fp_out=fopen("D:\\cProgram\\data_out.txt","w+"))==NULL)
	{printf("open file error!\n");
		exit(0);
	}
	
	while((c=fgetc(fp_in))!=EOF)
	{fputc(c,fp_out);
	}
	fclose(fp_in);
	fclose(fp_out);
	return 0;
}

fgets_fputs

#include#include#includeint main()
{FILE *fp_in;
	FILE *fp_out;
	char str[3][10];
	char temp[10];
	int i=0;
	int j=0;
	printf("input 3 strings\n");
	for(i=0;i<3;i++)
	{scanf("%s",str[i]);
	}
	for(i=0;i<3;i++)
	{for(j=0;j<3-i-1;j++)
		{	if(strcmp(str[j],str[j+1])>0)
			{		strcpy(temp,str[j]);
				strcpy(str[j],str[j+1]);
				strcpy(str[j+1],temp);
			}
		}
	}
	
	if((fp_in=fopen("D:\\cProgram\\data_in.txt","w+"))==NULL)
	{printf("open file error!\n");
		exit(0);
	}
	for(i=0;i<3;i++)
	{fputs(str[i],fp_in);
		fputs("\n",fp_in);
	}
	fclose(fp_in);
	
	if((fp_in=fopen("D:\\cProgram\\data_in.txt","r+"))==NULL)
	{printf("open file error!\n");
		exit(0);
	}
	if((fp_out=fopen("D:\\cProgram\\data_out.txt","w+"))==NULL)
	{printf("open file error!\n");
		exit(0);
	}
	
	while(fgets(temp,10,fp_in)!=NULL)
	{fputs(temp,fp_out);
	}
	
	fclose(fp_in);
	fclose(fp_out);
	return 0;
}

fprintf_scanf

#include#include#includeint main()
{FILE *fp_in;
	FILE *fp_out;
	char str[3][10];
	char temp[10];
	int i=0;
	int j=0;
	printf("input 3 strings\n");
	for(i=0;i<3;i++)
	{scanf("%s",str[i]);
	}
	for(i=0;i<3;i++)
	{for(j=0;j<3-i-1;j++)
		{	if(strcmp(str[j],str[j+1])>0)
			{		strcpy(temp,str[j]);
				strcpy(str[j],str[j+1]);
				strcpy(str[j+1],temp);
			}
		}
	}
	
	if((fp_in=fopen("D:\\cProgram\\data_in.txt","w+"))==NULL)
	{printf("open file error!\n");
		exit(0);
	}
	for(i=0;i<3;i++)
	{fprintf(fp_in,"%s\n",str[i]);
	}
	fclose(fp_in);
	
	if((fp_in=fopen("D:\\cProgram\\data_in.txt","r+"))==NULL)
	{printf("open file error!\n");
		exit(0);
	}
	if((fp_out=fopen("D:\\cProgram\\data_out.txt","w+"))==NULL)
	{printf("open file error!\n");
		exit(0);
	}
	
	for(i=0;i<3;i++)
	{fscanf(fp_in,"%s",temp);
		fprintf(fp_out,"%s\n",temp);
	}
	
	fclose(fp_in);
	fclose(fp_out);
	return 0;
}

fwrite_fread

#include#includestruct student
{char name[16];
	int num;
	int age;
}stu[5];

int main()
{FILE *fp;
	int i=0;
	int size=sizeof(stu[0]);
	struct student temp;
	char str[]="hello world\n";
	fwrite(str,sizeof(str),1,stdout);
	if((fp=fopen("D:\\cProgram\\student.dat","wb+"))==NULL)
	{printf("file open error!\n");
		exit(0);
	}
	
	for(i=0;i<3;i++)
	{printf("please input stu%d's name\n",i);
		scanf("%s",stu[i].name);
		printf("please input stu%d's num\n",i);
		scanf("%d",&stu[i].num);
		printf("please input stu%d's age\n",i);
		scanf("%d",&stu[i].age);
		
		fwrite(&stu[i],size,1,fp);
	}
	fclose(fp);
	
	if((fp=fopen("D:\\cProgram\\student.dat","rb+"))==NULL)
	{printf("file open error!\n");
		exit(0);
	}
	for(i=0;i<3;i++)
	{fread(&temp,size,1,fp);
		printf("stu%d:\nname:%s num:%d age:%d\n",i,stu[i].name,stu[i].num,stu[i].age);
	}
	fclose(fp);
	return 0;
}

fseek

#include#includestruct student
{char name[16];
	int num;
	int age;
};

int main()
{FILE *fp;
	struct student temp;
	int size=sizeof(temp);
	
	if((fp=fopen("D:\\cProgram\\student.dat","rb+"))==NULL)
	{printf("file open error!\n");
		exit(0);
	}
	
	fseek(fp,-size*3,2);
	fseek(fp,size,1);
	fread(&temp,size,1,fp);
	printf("name:%s num:%d age:%d",temp.name,temp.num,temp.age);
	fclose(fp);
	return 0;
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站栏目:C语言文件操作函数大全-创新互联
分享路径:http://6mz.cn/article/dpiceo.html

其他资讯