網站首頁 編程語言 正文
文件(file)通常是在磁盤或固態硬盤上的一段已命名的存儲區。C中采用的主要是文件指針的辦法,C++中對文件的操作主要運用了“文件流”(即非標準的輸入輸出)的思想。
一、C
C把文件看作是一系列連續的字節,每個字節都能被單獨讀取。C提供兩種文件模式:文本模式和二進制模式。
1.fopen函數
std::FILE* fopen( const char* filename, const char* mode );
打開文件filename
并返回與該文件關聯的文件流。mode
用于確定文件訪問模式。
參數?
filename | - | 將文件流關聯到的文件名 |
mode | - | 以空字符結尾的字符串確定文件訪問模式 |
返回值
如果成功,則返回一個指向控制已打開文件流的對象的指針,同時清除 eof 和錯誤位。出錯時,返回一個空指針。
2.fclose
int fclose(FILE *stream)
C 庫函數?int fclose(FILE *stream)?關閉流 stream。刷新所有的緩沖區。
參數
stream?是指向 FILE 對象的指針,該 FILE 對象指定了要被關閉的流。
返回值
如果流成功關閉,則該方法返回零。如果失敗,則返回 EOF。
3.FILE結構體
C語言的stdio.h頭文件中定義了用于文件操作的結構體FILE。通過fopen返回一個文件指針(指向FILE結構體的指針)來進行文件操作。可以在stdio.h頭文件中查看FILE結構體的定義,如下:
?不過在visual studio中貌似被隱藏了,見https://docs.microsoft.com/en-us/cpp/porting/visual-cpp-change-history-2003-2015
4.fprintf()和fscanf()函數
文件I/O函數 fprintf() 和 fscanf() 函數的工作方式與 printf() 和 scanf()類 似,區別在于前者需要用第1個參數指定待處理的文件。
int fprintf(FILE *stream, const char *format, ... ); fprintf()函數根據指定的format(格式)發送信息(參數)到由stream(流)指定的文件. int fscanf(FILE *stream, const char *format, ...) C庫函數 int fscanf(FILE *stream, const char *format, ...) 從流stream讀取格式化輸入。
- stream?-- 指向 FILE 對象的指針,該 FILE 對象標識了流。
- format?-- C 字符串,包含了以下各項中的一個或多個:空格字符、非空格字符?和?format 說明符。
成功則返回成功賦值或寫入的個數,失敗或到達文件末尾返回負數。
二、C++
頭文件fstream 定義了三個類型來支持文件IO: ifstream從一個給定文件讀取數據,ofstream向一個給定文件寫入數據,以及fstream可以讀寫給定文件。
每個流都有一個關聯的文件模式(file mode),用來指出如何使用文件。表8.4列出了文件模式和它們的含義。
每個文件流類型都定義了一個默認的文件模式,當未指定文件模式時就使用此默認模式。與ifstream關聯的文件默認以in模式打開,與ofstream關聯的文件默認以out模式打開,與fstream關聯的文件默認以in和out模式打開。
默認情況下,打開一個ofstream時,文件的內容會被丟棄。阻止一個ofstream清空給定文件內容的方法是同時指定app或in模式:
//在這幾條語句中,filel都被截斷 ofstream out ("filel"); //隱含以輸出模式打開文件并截斷文件 ofstream out2("file1", ofstream::out); //隱含地截斷文件 ofstream out3("file1", ofstream::out | ofstream::trunc); //為了保留文件內容,顯式指定app模式 ofstream app("file2", ofstream::app); //隱含為輸出模式 ofstream app2("fi1e2", ofstream::out | ofstream::app);
三、示例程序
#include#include #include #include #include #include using namespace std; const int MAX_NUM = 100; int a[MAX_NUM]; int n = 2; int main(int argc, char* argv[]) { FILE* file4 = fopen("d.txt", "w"); if (!file4) { printf("file4 open error!\n"); return -1; } //fprintf(file4, "name age sex position\n"); int age, sex; char name[50], position[50]; printf("please input 2 date : name age sex position:\n"); while (n--) { scanf("%s %d %d %s", &name, &age, &sex, &position); fprintf(file4, "%s %d %d %s\n", name, age, sex, position); } fclose(file4); FILE* file5 = fopen("d.txt", "r"); if (!file5) { printf("file5 open error!"); return -1; } int m = 2, ans, ans1; while (m--) { fscanf(file5, "%s %d %d %s", &name, &age, &sex, &position); printf("%s %d %d %s\n", name, age, sex, position); } fclose(file5); fstream file1("d.txt"); if (!file1) { cout << "file1 open error! " << endl; return -1; } string tmp; vector str; while (file1 >> tmp) { str.push_back(tmp); cout << tmp << endl; } ifstream file2("b.txt", ios::in); if (!file2) { cout << "file2 open error! " << endl; return -1; } ofstream file3("c.txt", ios::out); if (!file3) { cout << "file3 open error! " << endl; return -1; } while (n--) { file3 << n<<' '; } file1.close(); file2.close(); file3.close(); return 0; }
輸入輸出結果如下:
總結
原文鏈接:https://blog.csdn.net/qq_44272681/article/details/122712161
相關推薦
- 2022-05-15 react底層的四大核心內容架構詳解_React
- 2022-12-11 Django切換數據庫和遷移數據詳解_python
- 2022-03-14 YUV420SP to JPEG
- 2022-07-14 python求解三角形第三邊長實例_python
- 2022-08-01 詳解Selenium中元素定位方式_python
- 2022-11-12 Shell實現字符串處理的方法詳解_linux shell
- 2022-05-08 Python?matplotlib實現折線圖的繪制_python
- 2023-02-09 Python關鍵字?asynico基本用法_python
- 最近更新
-
- 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同步修改后的遠程分支