C語言檔案處理基礎
**檔案處理**是指使用程式將資料儲存到檔案中的過程。在C語言中,程式使用檔案處理將程式的結果和其他資料儲存到檔案中。此外,我們還可以從檔案中提取/獲取資料並在程式中使用它。
您可以在C語言檔案中執行的操作包括:
建立新檔案
開啟現有檔案
從現有檔案讀取資料
向檔案寫入資料
將資料移動到檔案中的特定位置
關閉檔案
使用fopen()建立或開啟檔案
fopen()函式用於在C語言中建立新檔案或開啟現有檔案。fopen函式在**stdio.h**標頭檔案中定義。
現在,讓我們看看建立新檔案或開啟檔案的語法。
file = fopen(“file_name”, “mode”)
這是在C語言中開啟和建立檔案的常用語法。
引數
file_name − 這是一個字串,指定使用fopen方法要開啟或建立的檔名。mode: 這是一個字串(通常是一個字元),指定開啟檔案的模式。C語言中有多種開啟檔案的方式,我們將在本文後面學習所有這些方式。
何時會建立檔案?
當fopen函式在指定位置找不到指定名稱的檔案時,它將建立一個新檔案。否則,如果找到該檔案,它將以指定的模式開啟它。
讓我們來看一個示例,這將使概念更清晰。假設我們使用fopen函式開啟名為hello.txt的檔案。語句如下:
file = fopen(“hello.txt”, “w”)
這將在當前目錄中搜索名為hello.txt的檔案。如果檔案存在,它將開啟該檔案;否則,它將建立一個名為“hello.txt”的新檔案,並以寫入模式(使用“w”指定)開啟它。
現在,讓我們看看所有可用於讀取或寫入C語言檔案的模式,並檢視顯示程式碼示例執行的程式碼片段。
**Mode = “r”** − 只讀開啟,此模式僅以讀取目的開啟檔案,即只能檢視內容,不能進行任何其他操作,例如編輯。
此模式無法建立新檔案,如果我們嘗試使用此模式建立新檔案,則**open()返回NULL**。
示例
#include <stdio.h> int main(){ FILE * file; if (file = fopen("hello.txt", "r")){ printf("File opened successfully in read mode"); } else printf("The file is not present! cannot create a new file using r mode"); fclose(file); return 0; }
輸出
File opened successfully in read mode
我們在當前目錄中建立了一個名為hello.txt的檔案,但如果我們嘗試訪問其他檔案,則輸出將為“檔案不存在!無法使用r模式建立新檔案”。
**Mode = “rb”** − 以二進位制模式開啟以進行讀取,此模式僅以二進位制模式開啟檔案以進行讀取,即只能檢視內容,不能進行任何其他操作,例如編輯。
此模式無法建立新檔案,如果我們嘗試使用此模式建立新檔案,則**open()返回NULL**。
示例
#include <stdio.h> int main(){ FILE * file; if (file = fopen("program.txt", "rb")){ printf("File opened successfully in read mode"); } else printf("The file is not present! cannot create a new file using rb mode"); fclose(file); return 0; }
輸出
檔案不存在!無法使用rb模式建立新檔案
**Mode = “w”** − 只寫開啟,此模式將僅以寫入方式開啟當前目錄中存在的,即無法執行讀取操作。如果當前目錄中不存在該檔案,則程式將建立一個新檔案並以寫入方式開啟它。
如果我們開啟一個包含某些文字的檔案,則內容將被覆蓋。
示例
#include <stdio.h> int main(){ FILE * file; if (file = fopen("helo.txt", "w")){ printf("File opened successfully in write mode or a new file is created"); } else printf("Error!"); fclose(file); return 0; }
輸出
File opened successfully in write mode or a new file is created
您可以在這裡看到,我們嘗試開啟目錄中不存在的檔案“helo.txt”,該函式仍然返回成功訊息,因為它建立了一個名為“helo.txt”的檔案。
**Mode = “wb”** − 以二進位制模式開啟以進行寫入,此模式將僅以二進位制寫入模式開啟當前目錄中存在的,即無法執行讀取操作。如果當前目錄中不存在該檔案,則程式將建立一個新檔案並以二進位制寫入模式開啟它。
如果我們開啟一個包含某些文字的檔案,則內容將被覆蓋。
示例
#include <stdio.h> int main(){ FILE * file; if (file = fopen("hello.txt", "wb")){ printf("File opened successfully in write in binary mode or a new file is created"); } else printf("Error!"); fclose(file); return 0; }
輸出
檔案已成功以二進位制寫入模式開啟或建立了一個新檔案
**Mode = “a”** − 只追加開啟,此模式將僅以寫入方式開啟當前目錄中存在的,即無法執行讀取操作。如果當前目錄中不存在該檔案,則程式將建立一個新檔案並以寫入方式開啟它。如果我們開啟一個包含某些文字的檔案,則內容不會被覆蓋;而是將新文字新增到檔案中的現有文字之後。
示例
#include <stdio.h> int main(){ FILE * file; if (file = fopen("hello.txt", "a")){ printf("File opened successfully in append mode or a new file is created"); } else printf("Error!"); fclose(file); return 0; }
輸出
File opened successfully in append mode or a new file is created
**Mode = “ab”** − 以二進位制模式開啟以進行追加,此模式將僅以二進位制寫入方式開啟當前目錄中存在的,即無法執行讀取操作。如果當前目錄中不存在該檔案,則程式將建立一個新檔案並以二進位制寫入模式開啟它。
如果我們開啟一個包含某些文字的檔案,則內容不會被覆蓋;而是將新文字新增到檔案中的現有文字之後。
示例
#include <stdio.h> int main(){ FILE * file; if (file = fopen("hello.txt", "ab")){ printf("File opened successfully in append in binary mode or a new file is created"); } else printf("Error!"); fclose(file); return 0; }
輸出
File opened successfully in append in binary mode or a new file is created
**Mode = “r+”** − 讀寫開啟,此模式將同時用於讀取和寫入目的開啟檔案,即可以對檔案執行讀取和寫入操作。
此模式無法建立新檔案,如果我們嘗試使用此模式建立新檔案,則**open()返回NULL**。
如果我們開啟一個包含某些文字的檔案並寫入某些內容,則內容將被覆蓋。
示例
#include <stdio.h> int main(){ FILE * file; if (file = fopen("hello.txt", "r+")){ printf("File opened successfully in read and write both"); } else printf("The file is not present! cannot create a new file using r+ mode"); fclose(file); return 0; }
輸出
File opened successfully in read and write both
我們在當前目錄中建立了一個名為hello.txt的檔案,但如果我們嘗試訪問另一個檔案,則輸出將為“檔案不存在!無法使用r+模式建立新檔案”。
**Mode = “rb+”** − 以二進位制模式開啟以進行讀取,此模式僅以二進位制模式開啟檔案以進行讀取,即只能檢視內容,不能進行任何其他操作,例如編輯。
此模式無法建立新檔案,如果我們嘗試使用此模式建立新檔案,則**open()返回NULL**。
如果我們開啟一個包含某些文字的檔案並寫入某些內容,則內容將被覆蓋。
示例
#include <stdio.h> int main(){ FILE * file; if (file = fopen("program.txt", "rb+")){ printf("File opened successfully in read mode"); } else printf("The file is not present! cannot create a new file using rb+ mode"); fclose(file); return 0; }
輸出
The file is not present! cannot create a new file using rb+ mode
**Mode = “w+”** − 讀寫開啟,此模式將開啟當前目錄中存在的用於寫入和讀取操作的檔案。如果當前目錄中不存在該檔案,則程式將建立一個新檔案並以讀取和寫入方式開啟它。
如果我們開啟一個包含某些文字的檔案,則內容將被覆蓋。
示例
#include <stdio.h> int main(){ FILE * file; if (file = fopen("helo.txt", "w+")){ printf("File opened successfully in read-write mode or a new file is created"); } else printf("Error!"); fclose(file); return 0; }
輸出
File opened successfully in read-write mode or a new file is created
您可以在這裡看到,我們嘗試開啟目錄中不存在的檔案“helo.txt”,該函式仍然返回成功訊息,因為它建立了一個名為“helo.txt”的檔案。
Mode = “wb+”:以二進位制模式開啟以進行寫入和讀取,此模式將開啟當前目錄中存在的用於以二進位制方式寫入和讀取的檔案。
如果當前目錄中不存在該檔案,則程式將建立一個新檔案並以二進位制模式開啟它以進行讀取和寫入。如果我們開啟一個包含某些文字的檔案,則內容將被覆蓋。
示例
#include <stdio.h> int main(){ FILE * file; if (file = fopen("hello.txt", "wb+")){ printf("File opened successfully in read-write in binary mode or a new file is created"); } else printf("Error!"); fclose(file); return 0; }
輸出
File opened successfully in read-write in binary mode or a new file is created
**Mode = “a+”** − 讀寫追加開啟,此模式將同時用於讀取和寫入開啟當前目錄中存在的。如果當前目錄中不存在該檔案,則程式將建立一個新檔案並以讀取和寫入方式開啟它。
如果我們開啟一個包含某些文字的檔案,則內容不會被覆蓋;而是將新文字新增到檔案中的現有文字之後。
示例
#include <stdio.h> int main(){ FILE * file; if (file = fopen("hello.txt", "a+")){ printf("File opened successfully in read-append mode or a new file is created"); } else printf("Error!"); fclose(file); return 0; }
輸出
檔案已成功以讀追加模式開啟或建立了一個新檔案
**Mode = “ab+”** − 以二進位制模式開啟以進行讀寫追加,此模式將同時用於以二進位制方式讀取和寫入開啟當前目錄中存在的。如果當前目錄中不存在該檔案,則程式將建立一個新檔案並以二進位制模式開啟它以進行讀取和寫入。如果我們開啟一個包含某些文字的檔案,則內容不會被覆蓋;而是將新文字新增到檔案中的現有文字之後。
示例
#include <stdio.h> int main(){ FILE * file; if (file = fopen("hello.txt", "ab+")){ printf("File opened successfully in read-append in binary mode or a new file is created"); } else printf("Error!”); fclose(file); return 0; }
輸出
File opened successfully in read-append mode or a new file is created
從現有檔案讀取資料
我們可以使用fscanf()、fgets()和fgetc()函式在c語言中讀取檔案的內容。所有這些都用於讀取檔案的內容。讓我們看看每個函式的工作原理:
fscanf()
fscanf()函式用於從檔案中讀取字元集,即字串。當它讀取完檔案的所有內容時,它返回EOF。
語法
int fscanf(FILE *stream, const char *charPointer[])
引數
FILE *stream: the pointer to the opened file. const char *charPointer[]: string of character.
示例
#include <stdio.h> int main(){ FILE * file; char str[500]; if (file = fopen("hello.txt", "r")){ while(fscanf(file,"%s", str)!=EOF){ printf("%s", str); } } else printf("Error!”); fclose(file); return 0; }
輸出
LearnprogrammingattutorialsPoint
fgets()
C語言中的fget()函式用於從流中讀取字串。
語法
char* fgets(char *string, int length, FILE *stream)
引數
char *string: It is a string which will store the data from the string. int length: It is an int which gives the length of string to be considered. FILE *stream: It is the pointer to the opened file.
示例
#include <stdio.h> int main(){ FILE * file; char str[500]; if (file = fopen("hello.txt", "r")){ printf("%s", fgets(str, 50, file)); } fclose(file); return 0; }
輸出
Learn programming at tutorials Point
fgetc()
C語言中的fgetc()函式用於從檔案中返回單個字元。它從檔案中獲取一個字元,並在檔案結尾返回EOF。
語法
char* fgetc(FILE *stream)
引數
FILE *stream: It is the pointer to the opened file.
示例
#include <stdio.h> int main(){ FILE * file; char str; if (file = fopen("hello.txt", "r")){ while((str=fgetc(file))!=EOF) printf("%c",str); } fclose(file); return 0; }
輸出
Learn programming at tutorials Point
向C語言檔案寫入資料
我們可以使用fprintf()、fputs()、fputc()函式向C語言檔案寫入資料。所有這些都用於向檔案寫入內容。
讓我們看看每個函式的工作原理:
fprintf()
fprintf()函式用於向檔案寫入資料。它將一組字元寫入檔案。
語法
int fprintf(FILE *stream, char *string[])
引數
FILE for *stream: It is the pointer to the opened file. char *string[]: It is the character array that we want to write in the file.
示例
#include <stdio.h> int main(){ FILE * file; if (file = fopen("hello.txt", "w")){ if(fprintf(file, "tutorials Point”) >= 0) printf("Write operation successful"); } fclose(file); return 0; }
輸出
Write operation successful
fputf()
C語言中的fputf()函式可用於向檔案寫入資料。它用於將一行(字元行)寫入檔案。
語法
int fputs(const char *string, FILE *stream)
引數
Constant char *string[]: It is the character array that we want to write in the file. FILE for *stream: It is the pointer to the opened file.
示例
#include <stdio.h> int main(){ FILE * file; if (file = fopen("hello.txt", "w")){ if(fputs("tutorials Point", file) >= 0) printf("String written to the file successfully..."); } fclose(file); return 0; }
輸出
String written to the file successfully…
fputc()
fputc()函式用於向檔案寫入單個字元。
語法
int fputc(char character , FILE *stream)
引數
char character : It is the character that we want to write in the file. FILE for *stream: It is the pointer to the opened file.
示例
#include <stdio.h> int main(){ FILE * file; if (file = fopen("hello.txt", "w")){ fputc('T', file); } fclose(file); return 0; }
輸出
‘T’ is written to the file.
fclose()
fclose()函式用於關閉開啟的檔案。我們應該在對檔案執行操作後關閉該檔案,以儲存我們對其應用的操作。
語法
fclose(FILE *stream)
引數
FILE for *stream: It is the pointer to the opened file.
示例
#include <stdio.h> int main(){ FILE * file; char string[300]; if (file = fopen("hello.txt", "a+")){ while(fscanf(file,"%s", string)!=EOF){ printf("%s", string); } fputs("Hello", file); } fclose(file); return 0; }
輸出
LearnprogrammingatTutorialsPoint
檔案內容:
Learn programming at Tutorials PointHello