1. 文件IO總結
文件IO操作: 對文件系統里的文件進行: 打開、創建、讀、寫、關閉等運用。
C語言下標準文件IO接口(函數):
(1)頭文件: stdio.h 輸入輸出函數: printf 、scanf
(2)相關函數: fopen、fread、fwrite、fclose
2.1 標準文件操作有兩套函數:
1.標準C語言下的文件操作接口。fopen系列
常用于: 對普通文件的讀寫。
2.Linux操作系統下的文件操作接口。open系列
常用于: 對設備文件進行讀寫。 (鼠標、鍵盤、聲卡、..)
2. C語言標準文件操作接口
2.1 最常用的4個函數
#include
//打開文件
FILE *fopen(const char *path, const char *mode);
//讀文件
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
//寫文件
size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
//關閉文件
int fclose(FILE *fp);
2.3 寫函數的基本運用
#include
#include
#include
int main()
{
FILE *file;
int cnt;
/*1. 打開文件*/
file=fopen("D:/123.txt","a+b");
if(file==NULL)
{
printf("文件打開失敗!\n");
return -1;
}
/*2. 寫數據*/
cnt=fwrite("1234567890",1,10,file);
/*3. 關閉文件*/
fclose(file);
printf("cnt=%d\n",cnt);
return 0;
}
2.4 讀函數基本運用
#include
#include
#include
int main()
{
FILE *file;
int cnt;
char buff[100];
/*1. 打開文件*/
file=fopen("D:/123.txt","rb"); //malloc
if(file==NULL)
{
printf("文件打開失敗!\n");
return -1;
}
/*2. 寫數據*/
cnt=fread(buff,1,100,file);
/*3. 關閉文件*/
fclose(file); //free
buff[cnt]='\0';
printf("%s\n",buff);
printf("cnt=%d\n",cnt);
return 0;
}
2.5 文件指針位置偏移 (自動向后偏移)
#include
#include
#include
int main()
{
FILE *file;
int cnt;
char data;
/*1. 打開文件*/
file=fopen("D:/123.txt","rb"); //malloc
if(file==NULL)
{
printf("文件打開失敗!\n");
return -1;
}
/*2. 讀數據---驗證文件指針是否可否自動向后偏移*/
cnt=fread(&data,1,1,file);
printf("data=%c\n",data);
cnt=fread(&data,1,1,file);
printf("data=%c\n",data);
cnt=fread(&data,1,1,file);
printf("data=%c\n",data);
cnt=fread(&data,1,1,file);
printf("data=%c\n",data);
cnt=fread(&data,1,1,file);
printf("data=%c\n",data);
/*3. 關閉文件*/
fclose(file); //free
return 0;
}
2.6 設置文件指針位置
#include
#include
#include
int main()
{
FILE *file;
int cnt;
char data;
/*1. 打開文件*/
file=fopen("D:/123.txt","rb"); //malloc
if(file==NULL)
{
printf("文件打開失敗!\n");
return -1;
}
/*2. 偏移文件指針*/
fseek(file,5,SEEK_SET);
/*3. 讀數據---驗證文件指針是否可否自動向后偏移*/
cnt=fread(&data,1,1,file);
printf("data=%c\n",data);
/*4. 關閉文件*/
fclose(file); //free
return 0;
}
2.7 以上午所學的函數,如何判斷文件讀完了?到文件結尾?
#include
#include
#include
int main()
{
FILE *file;
int cnt;
char data;
/*1. 打開文件*/
file=fopen("D:/123.txt","rb"); //malloc
if(file==NULL)
{
printf("文件打開失敗!\n");
return -1;
}
/*2. 偏移文件指針*/
fseek(file,5,SEEK_SET);
/*3. 讀數據---驗證文件指針是否可否自動向后偏移*/
while(1)
{
cnt=fread(&data,1,1,file);
if(cnt!=1)break;
printf("data=%c\n",data);
}
/*4. 關閉文件*/
fclose(file); //free
return 0;
}
2.8 文件讀寫結構體數據
//寫結構體數據
#include
#include
#include
struct MyStruct
{
int a;
int b;
char c[100];
};
int main()
{
FILE *file;
int cnt;
struct MyStruct stu={666,888,"C語言文件操作學習"};
/*1. 打開文件*/
file=fopen("D:/123.txt","wb");
if(file==NULL)
{
printf("文件打開失敗!\n");
return -1;
}
/*2. 讀數據*/
cnt=fwrite(&stu,1,sizeof(struct MyStruct),file);
printf("cnt=%d\n",cnt);
/*3. 關閉文件*/
fclose(file); //free
return 0;
}
//讀結構體數據
#include
#include
#include
struct MyStruct
{
int a;
int b;
char c[100];
};
int main()
{
FILE *file;
int cnt;
struct MyStruct stu;
/*1. 打開文件*/
file=fopen("D:/123.txt","rb");
if(file==NULL)
{
printf("文件打開失敗!\n");
return -1;
}
/*2. 讀數據*/
cnt=fread(&stu,1,sizeof(struct MyStruct),file);
printf("cnt=%d\n",cnt);
printf("%d,%d,%s\n",stu.a,stu.b,stu.c);
/*3. 關閉文件*/
fclose(file); //free
return 0;
}
2.9 文件操作的作業練習
1. 學習文件基本讀寫使用
2. 編寫文件拷貝程序。 實現文件拷貝。
3. 文件加密解密實現。 需要編寫一個菜單。
4. 完善學生管理系統。
需要將所有學生信息保存到文件里,完善功能。
審核編輯:湯梓紅
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
C語言
+關注
關注
180文章
7614瀏覽量
137704 -
文件系統
+關注
關注
0文章
287瀏覽量
19974 -
函數
+關注
關注
3文章
4346瀏覽量
62968
發布評論請先 登錄
相關推薦
stm32中的io函數 STM32中的IO操作
STM32系列單片機芯片的庫函數有很多種,而負責IO操作的庫函數是GPIO,GPIO函數在STM32單片機頭
c語言gets函數可以輸入數字嗎
C語言中的gets函數是用來讀取字符串的,而不是用來讀取數字的。它會讀取輸入的字符直到遇到換行符或者文件結束符。因此,如果你嘗試使用gets函數
評論