C語言文件操作函數(shù)大全-創(chuàng)新互聯(lián)

fgetc_fputc

成都服務(wù)器托管,成都創(chuàng)新互聯(lián)公司提供包括服務(wù)器租用、成都天府聯(lián)通服務(wù)器托管、帶寬租用、云主機(jī)、機(jī)柜租用、主機(jī)租用托管、CDN網(wǎng)站加速、申請域名等業(yè)務(wù)的一體化完整服務(wù)。電話咨詢: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;
}

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)站名稱:C語言文件操作函數(shù)大全-創(chuàng)新互聯(lián)
本文URL:http://muchs.cn/article40/dpiceo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、服務(wù)器托管外貿(mào)建站、響應(yīng)式網(wǎng)站網(wǎng)站建設(shè)、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站