網站首頁 編程語言 正文
一、文件操作
1、 為什要使用文件操作
在c語言中我們完成一個程序后,他并不會對我們的數據進行保存,就像我上一篇文章中實現一個通訊錄人員的增刪插改中數據只是短暫的保存,而當程序退出后的同時人員信息也就銷毀了,在此執行程序的時候人員信息并沒有保存,而這是為什呢?是因為我們執行程序時我們寫的代碼信息數據不是持久化的,而要讓他持久化就要用到文件操作,將我們寫的數據存到文件中,而文件信息是存放到硬盤當中的,這樣就達到了信息的持久化,也就在此使用時信息還是進行了一個保存。
2、什么是文件
2.1概念
存放在磁盤的文件就是文件。文件分為兩種:數據文件,程序文件。
2.1.1程序文件:
包括源程序文件(后綴為.c),目標文件(windows環境后綴為.obj),可執行程序(windows環境 后綴為.exe)。
2.1.2數據文件:
文件的內容不一定是程序,而是程序運行時讀寫的數據,比如程序運行需要從中讀取數據的文件, 或者輸出內容的文件。
2.2文件名
一個文件要有一個唯一的文件標識,以便用戶識別和引用。
文件名包含3部分:文件路徑+文件名主干+文件后綴
例如: c:\code\test.txt 為了方便起見,文件標識常被稱為文件名。
3、文件操作的使用
3.1文件操作的打開和關閉
文件在讀寫之前應該先打開文件,在使用結束之后應該關閉文件。 在編寫程序的時候,在打開文件的同時,都會返回一個FILE*的指針變量指向該文件,也相當于建立了指 針和文件的關系。
ANSIC 規定使用fopen函數來打開文件,fclose來關閉文件。 ?
#include <stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE*p=fopen("Contact.txt","w"); //打開文件輸入文件地址,和打開方式
if(p==NULL) //如果傳入空指針則會報錯
{
printf("%s", strerror(errno));
}
fclose(p); //關閉文件
p=NULL; //防止變為野指針
return 0;
}
3.2文件使用方式為
3.3文件的順序讀寫
3.3.1fputc:
#include <stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE*p=fopen("Contact.txt","w");
if(p==NULL)
{
printf("%s", strerror(errno));
}
for(char i='a';i<'z';i++)
{
fputc(i,p); //像文件中寫入數據,寫入的是單個字節
}
fclose(p);
p=NULL;
return 0;
}
3.3.2 fgetc:
#include <stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE*p=fopen("Contact.txt","r");
if(p==NULL)
{
printf("%s", strerror(errno));
}
for(char i='a';i<'z';i++)
{
printf("%c ",fgetc(p)); //從文件中獲得數據打印出來
}
fclose(p);
p=NULL;
return 0;
}
3.3.3 fputs:
#include <stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE*p=fopen("Contact.txt","w");//寫入文件數據用w,讀取數據用r
if(p==NULL)
{
printf("%s", strerror(errno));
}
fputs("hello world ",p); //向代碼中寫入字符串
fclose(p);
p=NULL;
return 0;
}
3.3.4fgets:
#include <stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE*p=fopen("Contact.txt","r");//寫入文件數據用w,讀取數據用r
if(p==NULL)
{
printf("%s", strerror(errno));
}
char arr[20]={0};
fgets(arr,15,p);
printf("%s\n",arr);
fclose(p);
p=NULL;
return 0;
}
3.3.5fprintf:
#include <stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE*p=fopen("Contact.txt","w");//寫入文件數據用w,讀取數據用r
if(p==NULL)
{
printf("%s", strerror(errno));
}
char arr[20]={"hello xiaoma"};
fprintf(p,"%s",arr);
fclose(p);
p=NULL;
return 0;
}
3.3.6fscanf:
#include <stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE*p=fopen("Contact.txt","r");//寫入文件數據用w,讀取數據用r
if(p==NULL)
{
printf("%s", strerror(errno));
}
char arr[20]={0};
fscanf(p,"%s",arr);
printf("%s\n",arr);
fclose(p);
p=NULL;
return 0;
}
3.3.7fwrite:
#include <stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE*p=fopen("Contact.txt","wb");//寫入文件數據用w,讀取數據用r wb是以二進制的方式寫,rb是以二進制的方式讀
if(p==NULL)
{
printf("%s", strerror(errno));
}
char arr[20]={"hello world"};
for(int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
{
fwrite(arr+i,sizeof(arr[0]),1,p); //一次寫入一個元素,寫入類型為char型
}
fclose(p);
p=NULL;
return 0;
}
3.3.8fread:
#include <stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE*p=fopen("Contact.txt","rb");//寫入文件數據用w,讀取數據用r wb是以二進制的方式寫,rb是以二進制的方式讀
if(p==NULL)
{
printf("%s", strerror(errno));
}
char arr[20]={0};
fread(arr,sizeof(arr),1,p);
printf("%s\n",arr);
fclose(p);
p=NULL;
return 0;
}
3.4文件的隨機讀寫
3.4.1fseek:
根據文件指針的位置和偏移量來定位文件指針
#include <stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE*p=fopen("/Users/mamenghao/Desktop/Contact.txt","rb");//寫入文件數據用w,讀取數據用r wb是以二進制的方式寫,rb是以二進制的方式讀
if(p==NULL)
{
printf("%s", strerror(errno));
}
fseek(p,2,SEEK_SET); //SEEK_SET 偏移量從頭開始
// SEEK_CUR 偏移量從你上一次的位置開始
// SEEK_END //偏移量從尾開始
int ch =fgetc(p);
printf("%c ",ch);
fclose(p);
p=NULL;
return 0;
}
3.4.2ftell:
返回文件指針相對于起始位置的偏移量
#include <stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE*p=fopen("/Users/mamenghao/Desktop/Contact.txt","rb");//寫入文件數據用w,讀取數據用r wb是以二進制的方式寫,rb是以二進制的方式讀
if(p==NULL)
{
printf("%s", strerror(errno));
}
fseek(p,2,SEEK_SET); //SEEK_SET 偏移量從頭開始
// SEEK_CUR 偏移量從你上一次的位置開始
// SEEK_END //偏移量從尾開始
printf("%ld\n",ftell(p)); //這就打印了當前偏移量
int ch =fgetc(p);
printf("%c ",ch);
fclose(p);
p=NULL;
return 0;
}
3.4.3rewind:
讓文件指針的位置回到文件起始位置
#include <stdio.h>
#include<errno.h>
#include<string.h>
int main()
{
FILE*p=fopen("/Users/mamenghao/Desktop/Contact.txt","rb");//寫入文件數據用w,讀取數據用r wb是以二進制的方式寫,rb是以二進制的方式讀
if(p==NULL)
{
printf("%s", strerror(errno));
}
fseek(p,2,SEEK_SET); //SEEK_SET 偏移量從頭開始
// SEEK_CUR 偏移量從你上一次的位置開始
// SEEK_END //偏移量從尾開始
printf("%ld\n",ftell(p)); //這就打印了當前偏移量
rewind(p); //使偏移量回到了起始位置
printf("%ld\n",ftell(p)); //在次打印偏移量的位置來確定是否回到起始位置
int ch =fgetc(p);
printf("%c ",ch);
fclose(p);
p=NULL;
return 0;
}
總結
對于文件操作并不是特別難,主要是我們對于文件操作不習慣的原因,讓我們覺得文件操作很難,只要我么熟悉掌握fputc fgetc fputs fgets fprintf fscanf fwrite fread 這些函數的使用方法,文件操作也就容易理解啦,主要是在于操作,我們可以在下面把每一個都操作一下!!!
原文鏈接:https://blog.csdn.net/m0_63177573/article/details/127887572
相關推薦
- 2022-04-18 小程序中文本中間顯示連續的空格
- 2022-08-18 C++詳解實現Stack方法_C 語言
- 2022-03-16 Docker安裝Nginx問題及錯誤分析_docker
- 2023-02-09 Linux命令行循環執行shell命令_linux shell
- 2022-12-10 jquery異常問題Uncaught?TypeError:?$(...).on?is?not?a?f
- 2022-05-13 annot read properties of undefined (reading ‘split
- 2022-06-15 ASP.NET?MVC使用區域(Area)功能_基礎應用
- 2022-11-22 react?hooks實現原理解析_React
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支