C語言實(shí)現(xiàn)文件讀寫操作
本文實(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)文章
關(guān)于C++出現(xiàn)Bus error問題的排查與解決
項(xiàng)目代碼中經(jīng)常出現(xiàn)莫名其妙的Bus error問題,并且代碼中增加很多try catch 后依然不能將錯(cuò)誤捕獲,一旦Bus erro出現(xiàn),進(jìn)程直接崩潰掉,所以本文給大家介紹了關(guān)于C++出現(xiàn)Bus error問題的排查與解決,需要的朋友可以參考下2024-01-01
C++中的opeartor?new和placement?new使用步驟
這篇文章主要介紹了C++中的opeartor?new和placement?new詳解,在很多情況下,placement?new的使用方法和其他普通的new有所不同。這里提供了它的使用步驟,需要的朋友可以參考下2022-10-10
詳解C語言對字符串處理函數(shù)的實(shí)現(xiàn)方法
這篇文章主要為大家介紹了C語言對字符串處理函數(shù)的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-12-12
基于Sizeof與Strlen的區(qū)別以及聯(lián)系的使用詳解
本篇文章是對Sizeof與Strlen的區(qū)別以及聯(lián)系的使用進(jìn)行了詳細(xì)的介紹。需要的朋友參考下2013-05-05
C語言中隨機(jī)數(shù)rand()函數(shù)詳解
大家好,本篇文章主要講的是C語言中隨機(jī)數(shù)rand()函數(shù)詳解,感興趣的同學(xué)感快來看一看吧,對你有幫助的話記得收藏一下2022-02-02
如何使用arm-none-eabi-gcc編譯器搭建STM32的Vscode開發(fā)環(huán)境
這篇文章主要介紹了使用arm-none-eabi-gcc編譯器搭建STM32的Vscode開發(fā)環(huán)境,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07

