新聞中心
在C語(yǔ)言中,文件調(diào)用是非常重要的一個(gè)部分,它允許我們讀取和寫(xiě)入磁盤(pán)上的文件,通過(guò)文件調(diào)用,我們可以實(shí)現(xiàn)數(shù)據(jù)的持久化存儲(chǔ),以及在不同程序之間傳遞數(shù)據(jù),本文將詳細(xì)介紹C語(yǔ)言中文件調(diào)用的基本概念、操作流程以及一些常用的函數(shù)。

基本概念
1、文件指針:在C語(yǔ)言中,文件指針是一個(gè)指向FILE類(lèi)型的指針,用于標(biāo)識(shí)一個(gè)打開(kāi)的文件,當(dāng)我們打開(kāi)一個(gè)文件時(shí),系統(tǒng)會(huì)為這個(gè)文件分配一個(gè)FILE結(jié)構(gòu)體,并將文件指針指向這個(gè)結(jié)構(gòu)體。
2、文件類(lèi)型:C語(yǔ)言中有兩種文件類(lèi)型,分別是文本文件和二進(jìn)制文件,文本文件是指以字符為單位進(jìn)行讀寫(xiě)的文件,每個(gè)字符占一個(gè)字節(jié);二進(jìn)制文件是指以字節(jié)為單位進(jìn)行讀寫(xiě)的文件,可以讀寫(xiě)任意類(lèi)型的數(shù)據(jù)。
3、文件模式:C語(yǔ)言中有三種文件模式,分別是只讀模式(’r’)、只寫(xiě)模式(’w’)和讀寫(xiě)模式(’a’),只讀模式表示只能讀取文件內(nèi)容,不能寫(xiě)入;只寫(xiě)模式表示只能寫(xiě)入文件內(nèi)容,不能讀?。蛔x寫(xiě)模式表示既可以讀取文件內(nèi)容,也可以寫(xiě)入文件內(nèi)容。
操作流程
C語(yǔ)言中文件調(diào)用的基本操作流程如下:
1、打開(kāi)文件:使用fopen()函數(shù)打開(kāi)一個(gè)文件,并返回一個(gè)文件指針,如果打開(kāi)成功,fopen()函數(shù)返回非空指針;如果打開(kāi)失敗,fopen()函數(shù)返回NULL。
2、讀寫(xiě)文件:使用fread()、fwrite()等函數(shù)對(duì)文件進(jìn)行讀寫(xiě)操作。
3、關(guān)閉文件:使用fclose()函數(shù)關(guān)閉一個(gè)已經(jīng)打開(kāi)的文件,關(guān)閉文件后,不能再對(duì)這個(gè)文件進(jìn)行讀寫(xiě)操作。
常用函數(shù)
1、fopen()函數(shù):用于打開(kāi)一個(gè)文件,其原型為:FILE *fopen(const char *filename, const char *mode);
參數(shù)說(shuō)明:
filename:要打開(kāi)的文件名,可以是相對(duì)路徑或絕對(duì)路徑。
mode:打開(kāi)文件的模式,如"r"、"w"、"a"等。
返回值:成功打開(kāi)文件時(shí),返回一個(gè)非空的文件指針;失敗時(shí),返回NULL。
示例代碼:
#includeint main() { FILE *file = fopen("example.txt", "r"); if (file == NULL) { printf("Failed to open file. "); return 1; } // 對(duì)文件進(jìn)行讀寫(xiě)操作... fclose(file); // 關(guān)閉文件 return 0; }
2、fclose()函數(shù):用于關(guān)閉一個(gè)已經(jīng)打開(kāi)的文件,其原型為:int fclose(FILE *stream);
參數(shù)說(shuō)明:
stream:要關(guān)閉的文件指針。
返回值:成功關(guān)閉文件時(shí),返回0;失敗時(shí),返回EOF(通常是1)。
示例代碼:
#includeint main() { FILE *file = fopen("example.txt", "r"); if (file == NULL) { printf("Failed to open file. "); return 1; } // 對(duì)文件進(jìn)行讀寫(xiě)操作... if (fclose(file) != 0) { printf("Failed to close file. "); return 1; } return 0; }
3、fread()函數(shù):用于從文件中讀取數(shù)據(jù),其原型為:size_t fread(void *ptr, size_t size, size_t count, FILE *stream);
參數(shù)說(shuō)明:
ptr:指向要存儲(chǔ)數(shù)據(jù)的緩沖區(qū)的指針。
size:每個(gè)數(shù)據(jù)項(xiàng)的大小,以字節(jié)為單位。
count:要讀取的數(shù)據(jù)項(xiàng)個(gè)數(shù)。
stream:要讀取數(shù)據(jù)的文件指針。
返回值:實(shí)際讀取的數(shù)據(jù)項(xiàng)個(gè)數(shù),如果到達(dá)文件末尾或發(fā)生錯(cuò)誤,返回值可能小于count。
示例代碼:
#include#include #include #include #include // for read() function in Windows APIs (not needed in Linux/Mac) #ifdef _WIN32 // Windows specific code block: use the Win32 API function ReadFile() instead of fread() and fwrite() for reading and writing files. This is necessary because fread() and fwrite() are not standard C library functions on Windows. They are part of the C11 standard, but not all C compilers support them yet. The Win32 API function ReadFile() is similar to fread(), but it can also read data from a file that was opened with the "binary" or "text" mode, not just the "text" mode. Here is how you can use it: #define BUFSIZE 4096 int main() { HANDLE hFile; DWORD bytesRead; char buffer[BUFSIZE]; // Open the file in binary mode: hFile = CreateFile("example.bin", //GENERIC_READ, // GENERIC_WRITE, // 0, NULL, //OPEN_EXISTING, //FILE_ATTRIBUTE_NORMAL, NULL); // Read data from the file into the buffer: bytesRead = ReadFile(hFile, buffer, sizeof(buffer), NULL, NULL); if (bytesRead > 0) { // Process the data in the buffer... } else { // Error occurred: display an error message and exit the program. printf("Error %lu: %s when reading from file! GLE=", GetLastError(), strerror(GetLastError())); return 1; } // Close the file handle: CloseHandle(hFile); return 0; } #else // Unixlike systems specific code block: use fread() and fwrite() for reading and writing files. int main() { FILE *file = fopen("example.bin", "rb"); if (file == NULL) { perror("Failed to open file"); return 1; } // Read data from the file into the buffer: size_t bytesRead = fread(buffer, sizeof(char), sizeof(buffer), file); if (bytesRead > 0) { // Process the data in the buffer... } else { // Error occurred: display an error message and exit the program. perror("Error reading from file"); return 1; } // Close the file handle: fclose(file); return 0; } #endif void printBufferContents(const char *buffer) { for (size_t i = 0; i < strlen(buffer); i++) { printf("%c", buffer[i]); } } int main() { char buffer[BUFSIZE]; memset(buffer, 0, sizeof(buffer)); // Read data from the file into the buffer: size_t bytesRead = fread(buffer, sizeof(char), sizeof(buffer), stdin); if (bytesRead > 0) { // Process the data in the buffer... printBufferContents(buffer); } else { // Error occurred: display an error message and exit the program. perror("Error reading from file"); return 1; } return 0; } #endif // End of preprocessor directives (ifdef/ifndef). Note that this code block only works on Unixlike systems (Linux/macOS) and not on Windows. It uses the fread() function to read data from a file into a buffer, and then processes the data in the buffer using a custom printBufferContents() function that prints each character in the buffer on a new line. If an error occurs while reading from the file, it displays an error message and exits with a nonzero status code. If no error occurs, it prints the contents of the buffer and exits successfully with a zero status code.
分享題目:c語(yǔ)言中怎么文件調(diào)用
網(wǎng)站鏈接:http://m.fisionsoft.com.cn/article/djddjec.html


咨詢(xún)
建站咨詢(xún)
