<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # C 文件示例 > 原文: [https://www.programiz.com/c-programming/c-file-examples](https://www.programiz.com/c-programming/c-file-examples) #### 在本文中,您將找到處理 C 編程中文件輸入/輸出操作的示例列表。 要了解此頁面上的所有程序,您應該了解以下主題。 * [C 數組](/c-programming/c-arrays "C programming Arrays") * [C 指針](/c-programming/c-pointers "C Programming Pointers") * [數組和指針關系](/c-programming/c-pointers-arrays "Relation Between Arrays and Pointer") * [文件 I/O](/c-programming/c-file-input-output "C File Input Output") * * * ## C 文件示例 **1\. C 程序:讀取 n 個學生的姓名和分數并將它們存儲在文件中。** ```c #include <stdio.h> int main() { char name[50]; int marks, i, num; printf("Enter number of students: "); scanf("%d", &num); FILE *fptr; fptr = (fopen("C:\\student.txt", "w")); if(fptr == NULL) { printf("Error!"); exit(1); } for(i = 0; i < num; ++i) { printf("For student%d\nEnter name: ", i+1); scanf("%s", name); printf("Enter marks: "); scanf("%d", &marks); fprintf(fptr,"\nName: %s \nMarks=%d \n", name, marks); } fclose(fptr); return 0; } ``` **2\. C 程序:讀取 n 個學生的姓名和標志并將它們存儲在文件中。 如果文件先前已退出,則將信息添加到文件中。** ```c #include <stdio.h> int main() { char name[50]; int marks, i, num; printf("Enter number of students: "); scanf("%d", &num); FILE *fptr; fptr = (fopen("C:\\student.txt", "a")); if(fptr == NULL) { printf("Error!"); exit(1); } for(i = 0; i < num; ++i) { printf("For student%d\nEnter name: ", i+1); scanf("%s", name); printf("Enter marks: "); scanf("%d", &marks); fprintf(fptr,"\nName: %s \nMarks=%d \n", name, marks); } fclose(fptr); return 0; } ``` **3\. C 程序:使用`fwrite()`將結構數組的所有成員寫入文件。 從文件中讀取數組并顯示在屏幕上。** ```c #include <stdio.h> struct student { char name[50]; int height; }; int main(){ struct student stud1[5], stud2[5]; FILE *fptr; int i; fptr = fopen("file.txt","wb"); for(i = 0; i < 5; ++i) { fflush(stdin); printf("Enter name: "); gets(stud1[i].name); printf("Enter height: "); scanf("%d", &stud1[i].height); } fwrite(stud1, sizeof(stud1), 1, fptr); fclose(fptr); fptr = fopen("file.txt", "rb"); fread(stud2, sizeof(stud2), 1, fptr); for(i = 0; i < 5; ++i) { printf("Name: %s\nHeight: %d", stud2[i].name, stud2[i].height); } fclose(fptr); } ```
                  <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>

                              哎呀哎呀视频在线观看