<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 在 C 編程中進行文件 I/O > 原文: [https://beginnersbook.com/2014/01/c-file-io/](https://beginnersbook.com/2014/01/c-file-io/) 在本指南中,我們將學習如何使用 [C 編程](https://beginnersbook.com/2014/01/c-tutorial-for-beginners-with-examples/)語言對文件執行輸入/輸出(I/O)操作。 在我們詳細討論每個操作之前,讓我們來看一個簡單的 C 程序: ### 一個簡單的 C 程序,用于打開,讀取和關閉文件 ```c #include <stdio.h> int main() { /* Pointer to the file */ FILE *fp1; /* Character variable to read the content of file */ char c; /* Opening a file in r mode*/ fp1= fopen ("C:\\myfiles\\newfile.txt", "r"); /* Infinite loop –I have used break to come out of the loop*/ while(1) { c = fgetc(fp1); if(c==EOF) break; else printf("%c", c); } fclose(fp1); return 0; } ``` 在上面的程序中,我們在`r`模式下打開文件`newfile.txt`,讀取文件內容并在控制臺上顯示。讓我們詳細了解每個操作: ## 1\. 打開文件 `fopen()`函數用于打開文件。 **語法:** ```c FILE pointer_name = fopen ("file_name", "Mode"); ``` `pointer_name`可以是您選擇的任何東西。 `file_name`是您要打開的文件的名稱。在此處指定完整路徑,如`C:\\myfiles\\newfile.txt`。 打開文件時,需要指定模式。我們用來讀取文件的模式是`r`,它是“只讀模式”。 **例如:** ```c FILE *fp; fp = fopen("C:\\myfiles\\newfile.txt", "r"); ``` 第一個字符的地址存儲在`pointer fp`中。 **如何檢查文件是否已成功打開?** 如果文件未成功打開,則指針將被賦予`NULL`值,因此您可以編寫如下邏輯: 此代碼將檢查文件是否已成功打開。如果文件未打開,則會向用戶顯示錯誤消息。 ```c .. FILE fpr; fpr = fopen("C:\\myfiles\\newfile.txt", "r"); if (fpr == NULL) { puts("Error while opening file"); exit(); } ``` ### 各種文件打開模式: 使用`fopen()`函數打開文件,打開時可以根據需要使用以下任何一種模式。 **模式`r`**:這是一種只讀模式,這意味著如果文件在`r`模式下打開,它將不允許您編寫和修改它的內容。當`fopen()`成功打開文件時,它返回文件第一個字符的地址,否則返回`NULL`。 **模式`w`**:這是一種只寫模式。`fopen()`函數在指定文件不存在時創建新文件,如果無法打開文件,則返回`NULL`。 **模式`a`**:使用此模式,內容可以附加在現有文件的末尾。與模式`w`類似,如果文件不存在,`fopen()`會創建一個新文件。在打開不成功時,它返回`NULL`。文件指針指向:文件的最后一個字符。 **模式`r+`**:此模式與模式`r`相同;但是,您可以對在此模式下打開的文件執行各種操作。您可以讀取,寫入和修改以`r+`模式打開的文件內容。文件指針指向:文件的第一個字符。 **模式`w+`**:與可以執行的操作相同的模式`w`相同;可以在此模式下讀取,寫入和修改文件。 **模式`a+`**:與模式`a`相同;您可以在文件中讀取和附加數據,但在此模式下不允許進行內容修改。 ## 2\. 讀取文件 要讀取文件,我們必須首先使用任何模式打開它,例如,如果您只想讀取文件,然后以`r`模式打開它。根據文件打開期間選擇的模式,我們可以對文件執行某些操作。 ### C 程序:讀取文件 **`fgetc()`:**該函數從當前指針的位置讀取字符,成功讀取后,將指針移動到文件中的下一個字符。一旦指針到達文件的末尾,該函數返回 **EOF(文件結束)**。我們在程序中使用了 **EOF** 來確定文件的結尾。 ```c #include <stdio.h> int main() { /* Pointer to the file */ FILE *fp1; /* Character variable to read the content of file */ char c; /* Opening a file in r mode*/ fp1= fopen ("C:\\myfiles\\newfile.txt", "r"); /* Infinite loop –I have used break to come out of the loop*/ while(1) { c = fgetc(fp1); if(c==EOF) break; else printf("%c", c); } fclose(fp1); return 0; } ``` ## 3.寫入文件 要寫入文件,我們必須以支持寫入的模式打開文件。例如,如果以`r`模式打開文件,則無法寫入文件,因為`r`是只允許讀取的只讀模式。 ## 示例:C 程序寫入文件 該程序要求用戶輸入一個字符并將該字符寫在文件的末尾。如果該文件不存在,則該程序將創建具有指定名稱的文件,并將輸入字符寫入文件。 ```c #include <stdio.h> int main() { char ch; FILE *fpw; fpw = fopen("C:\\newfile.txt","w"); if(fpw == NULL) { printf("Error"); exit(1); } printf("Enter any character: "); scanf("%c",&ch); /* You can also use fputc(ch, fpw);*/ fprintf(fpw,"%c",ch); fclose(fpw); return 0; } ``` ## 4\. 關閉文件 ```c fclose(fp); ``` **`fclose()`**函數用于關閉打開的文件。作為參數,您必須提供指向要關閉的文件的指針。 ### 在 C 中顯示打開,讀取,寫入和關閉操作的示例 ```c #include <stdio.h> int main() { char ch; /* Pointer for both the file*/ FILE *fpr, *fpw; /* Opening file FILE1.C in “r” mode for reading */ fpr = fopen("C:\\file1.txt", "r"); /* Ensure FILE1.C opened successfully*/ if (fpr == NULL) { puts("Input file cannot be opened"); } /* Opening file FILE2.C in “w” mode for writing*/ fpw= fopen("C:\\file2.txt", "w"); /* Ensure FILE2.C opened successfully*/ if (fpw == NULL) { puts("Output file cannot be opened"); } /*Read & Write Logic*/ while(1) { ch = fgetc(fpr); if (ch==EOF) break; else fputc(ch, fpw); } /* Closing both the files */ fclose(fpr); fclose(fpw); return 0; } ``` ## 如何在文件中讀/寫(I/O)字符串 - `fgets`和`fputs` 在這里,我們將討論如何讀字符串和將其寫到文件中。 ```c char *fgets(char *s, int rec_len, FILE *fpr) ``` **`s`** :存儲字符串的字符數組。 **`rec_len`** :輸入記錄的長度。 **`fpr`** :指向輸入文件的指針。 讓我們舉一個例子: ### 在 C 編程中從文件中讀取字符串的示例 ```c #include <stdio.h> int main() { FILE *fpr; /*Char array to store string */ char str[100]; /*Opening the file in "r" mode*/ fpr = fopen("C:\\mynewtextfile.txt", "r"); /*Error handling for file open*/ if (fpr == NULL) { puts("Issue in opening the input file"); } /*Loop for reading the file till end*/ while(1) { if(fgets(str, 10, fpr) ==NULL) break; else printf("%s", str); } /*Closing the input file after reading*/ fclose(fpr); return 0; } ``` 在上面的例子中,我們使用了這樣的`fgets`函數: ```c fgets(str, 10, fpr) ``` 這里 **`str`**表示從文件中讀取字符串后,存儲字符串的緩沖區(`char`數組)。**`10`** 是每次需要讀取的字符串的長度。**`fpr`** 是指向文件的指針,將被讀取。 **為什么我用`if(fgets(str, 10, fpr) ==NULL)`作為判斷文件結尾的邏輯?** 在上面的例子中,我們用`ch == EOF`來知道文件的結尾。這里我們使用了這個邏輯,因為當沒有更多的記錄可供讀取時,`fgets`返回`NULL`。 ### C 程序 - 將字符串寫入文件 ```c int fputs ( const char * s, FILE * fpw ); ``` `char *s` - `char`數組。 `FILE *fpw` - 指向文件的指針(`FILE`類型),將被寫入。 ```c #include <stdio.h> int main() { FILE *fpw; /*Char array to store strings */ char str[100]; /*Opening the file FILEW.TXT in "w" mode for writing*/ fpw = fopen("C:\\mynewtextfile2.txt", "w"); /*Error handling for output file*/ if (fpw== NULL) { puts("Issue in opening the Output file"); } printf("Enter your string:"); /*Stored the input string into array – str*/ gets(str); /* Copied the content of str into file – * mynewtextfile2.txt using pointer – fpw */ fputs(str, fpw); /*Closing the Output file after successful writing*/ fclose(fpw); return 0; } ``` **`fputs`有兩個參數:** ```c fputs(str, fpw) ``` **`str`** - `str`表示存儲字符串的數組。 **`fpw`** - 指向輸出文件的`FILE`指針,需要在其中寫入記錄。 **關于`fputs`的注意事項:默認情況下** `fputs`在寫入每條記錄后不會添加新行,為了手動執行此操作 - 每次寫入文件后都可以使用以下語句。 ```c fputs("\n", fpw); ``` ## C 文件 I/O:二進制文件 到目前為止,我們已經學習了文本文件的文件操作,如果文件是二進制文件(例如`.exe`文件)。上述程序不適用于二進制文件,但處理二進制文件時有一些細微的變化。主要區別在于**文件名和模式。** 讓我們在一個例子的幫助下理解這一點。可以說我有兩個二進制文件`bin1.exe`和`bin2.exe`- 我想將`bin1.exe`的內容復制到`bin2.exe`: ### 示例:在 C 中讀取和寫入二進制文件 ```c #include <stdio.h> int main() { char ch; /* Pointers for both binary files*/ FILE *fpbr, *fpbw; /* Open for bin1.exe file in rb mode */ fpbr = fopen("bin1.exe", "rb"); /* test logic for successful open*/ if (fpbr == NULL) { puts("Input Binary file is having issues while opening"); } /* Opening file bin2.exe in “wb” mode for writing*/ fpbw= fopen("bin2.exe", "wb"); /* Ensure bin2.exe opened successfully*/ if (fpbw == NULL) { puts("Output binary file is having issues while opening"); } /*Read & Write Logic for binary files*/ while(1) { ch = fgetc(fpbr); if (ch==EOF) break; else fputc(ch, fpbw); } /* Closing both the binary files */ fclose(fpbr); fclose(fpbw); return 0; } ``` **注意:文件打開模式為`rb`和`wb`而不是`r`和`w`**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看