最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言實(shí)現(xiàn)文件讀寫操作

 更新時(shí)間:2020年12月28日 10:33:23   作者:零商  
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)文件讀寫操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C語言實(shí)現(xiàn)文件讀寫操作的具體代碼,供大家參考,具體內(nèi)容如下

鍵盤讀入字符串寫到文件中,再從文件讀出顯示在控制臺(tái)

#include<stdio.h>
#include<string.h>
int main()
{
 FILE *fp;
 char string[6];//方括號中是幾就輸入幾個(gè)字符串
 if( (fp=fopen("file.txt","w"))==NULL )
 {
 printf("cannot open file");
 return 0;
 }
 while(strlen(gets(string)) > 0)
 {
 fputs(string,fp);
 fputs("\n",fp);
 }
 fclose(fp);

 if( (fp=fopen("file.txt","r"))==NULL)
 {
 printf("cannot open file\n");
 return 0;
 }
 while(fgets(string,6,fp)!=NULL)
 {
 fputs(string,stdout);//系統(tǒng)自動(dòng)打開stdout文件
 }
 fclose(fp);
}

合并兩個(gè)文件的內(nèi)容,并輸出到第三個(gè)文件

#include<stdio.h>
#include<string.h>
int main()
{
 FILE *fp1,*fp2,*fp3;
 char str1[10],str2[10];
 printf("輸入兩串字母\n");
 scanf("%s",str1);
 scanf("%s",str2);
 
 //A,B兩個(gè)文件賦值
 if((fp1=fopen("A.txt","w"))==NULL)
 {
 printf("cannot open file\n");
 return 0;
 } 
 fputs(str1,fp1); 
 fclose(fp1);

 if((fp2=fopen("B.txt","w"))==NULL)
 {
 printf("cannot open file\n");
 return 0;
 } 
 fputs(str2,fp2); 
 fclose(fp2);
 
 //拷貝到第三個(gè)文件
 if((fp1=fopen("A.txt","r"))==NULL)
 {
 printf("cannot open file\n");
 return 0;
 }
 if((fp2=fopen("B.txt","r"))==NULL)
 {
 printf("cannot open file\n");
 return 0;
 }
 if((fp3=fopen("C.txt","a"))==NULL)
 {
 printf("cannot open file\n");
 return 0;
 }
 while(!feof(fp1))
 {
 fputc(fgetc(fp1),fp3);
 }
 while(!feof(fp2))
 {
 fputc(fgetc(fp2),fp3);
 }
 fclose(fp1);
 fclose(fp2);
 fclose(fp3);
}

輸入學(xué)生信息并轉(zhuǎn)存到磁盤文件

#include<stdio.h>
#define SIZE 4
struct student_type
{
 char name[10];
 int num;
 int age;
 char addr[15];
};
struct student_type stud[SIZE];

void save();
void display();
void main()
{
 int i;
 for(i=0;i<SIZE;i++)
 {
 scanf("%s %d %d %s",stud[i].name, &stud[i].num, &stud[i].age, stud[i].addr);
 }

 save();//轉(zhuǎn)存
 display();
}

void save()
{
 FILE *fp;
 int i;
 if((fp=fopen("E:\\計(jì)算機(jī)導(dǎo)論作業(yè)\\加密文檔","wb"))==NULL)
 {
 printf("cannot open file\n");
 return;
 }
 for(i=0;i<SIZE;i++)
 {
 if(fwrite(&stud[i], sizeof(struct student_type),1,fp)!=1)
  printf("file write error\n");
 }
 fclose(fp);
}

void display()
{
 FILE *fp;
 int i;
 if((fp=fopen("E:\\計(jì)算機(jī)導(dǎo)論作業(yè)\\加密文檔","rb"))==NULL)
 {
 printf("cannot open file\n");
 return;
 }
 for(i=0;i<SIZE;i++)
 {
 fread(&stud[i], sizeof(struct student_type), 1, fp);
 printf("%-10s %4d %4d %-15s\n",stud[i].name, stud[i].num, stud[i].age, stud[i].addr);
 }
 fclose(fp);
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

韩城市| 兰溪市| 香格里拉县| 恭城| 文昌市| 平泉县| 仁怀市| 灌阳县| 黔江区| 宣汉县| 锦州市| 武功县| 布拖县| 徐水县| 邵阳县| 隆尧县| 遂溪县| 当涂县| 瑞丽市| 襄汾县| 嵊州市| 兴安县| 黔西县| 景谷| 云霄县| 墨脱县| 洮南市| 阳江市| 神木县| 名山县| 济源市| 米泉市| 永昌县| 奎屯市| 弥勒县| 建德市| 原阳县| 平阴县| 莆田市| 桑植县| 务川|