C語言檔案處理



C語言檔案處理

C語言中的檔案處理是指使用C語言函式處理檔案操作的過程,例如建立、開啟、寫入資料、讀取資料、重新命名和刪除。藉助這些函式,我們可以在程式中執行檔案操作,以便在檔案中儲存和檢索資料。

C語言中檔案處理的必要性

如果我們使用C程式執行輸入和輸出操作,則資料僅在程式執行期間存在,當程式終止時,我們無法再次使用該資料。檔案處理需要處理儲存在外部儲存器中的檔案,即從計算機的外部儲存器儲存和訪問資訊。您可以使用檔案處理永久儲存資料。

檔案的型別

檔案表示一系列位元組。檔案有兩種型別:文字檔案二進位制檔案

  1. 文字檔案 - 文字檔案以ASCII字元的形式包含資料,通常用於儲存字元流。文字檔案中的每一行以換行符("\n")結尾,通常具有".txt"副檔名。
  2. 二進位制檔案 - 二進位制檔案以原始位(0和1)的形式包含資料。不同的應用程式程式有不同的表示位和位元組的方法,並使用不同的檔案格式。影像檔案(.png,.jpg),可執行檔案(.exe,.com)等是二進位制檔案的示例。

檔案指標 (File*)

在處理檔案處理時,您需要一個檔案指標來儲存fopen()函式返回的FILE結構的引用。所有檔案處理操作都需要檔案指標。

fopen()函式 返回FILE型別的指標。FILEstdio.h中預定義的結構體型別,包含檔案描述符、大小和位置等屬性。

typedef struct {
   int fd;			/* File descriptor */
   unsigned char *buf;	/* Buffer */
   size_t size;		/* Size of the file */
   size_t pos;		/* Current position in the file */
} FILE;

宣告檔案指標 (FILE*)

以下是宣告檔案指標的語法:

FILE* file_pointer;

開啟(建立)檔案

必須開啟檔案才能執行任何操作。fopen()函式用於建立新檔案或開啟現有檔案。您需要指定要開啟的模式。下面解釋了各種檔案開啟模式,在建立/開啟檔案時可以使用其中任何一種。

fopen()函式返回一個FILE指標,該指標將用於其他操作,例如讀取、寫入和關閉檔案。

語法

以下是開啟檔案的語法:

FILE *fopen(const char *filename, const char *mode);

這裡,filename是要開啟的檔案的名稱,mode定義檔案的開啟模式。

檔案開啟模式

檔案訪問模式預設以文字或ASCII模式開啟檔案。如果要處理二進位制檔案,則應使用以下訪問模式,而不是上述模式

"rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b"

檔案可以以多種模式開啟。以下是不同的檔案開啟模式:

模式 描述
r 以讀取目的開啟現有的文字檔案。
w 開啟文字檔案以進行寫入。如果檔案不存在,則建立一個新檔案。在此,您的程式將從檔案開頭開始寫入內容。
a 以追加模式開啟文字檔案以進行寫入。如果檔案不存在,則建立一個新檔案。在此,您的程式將開始將內容追加到現有檔案內容中。
r+ 開啟文字檔案以進行讀寫。
w+
a+ 開啟文字檔案以進行讀寫。如果檔案不存在,則建立檔案。讀取將從開頭開始,但只能追加寫入。

建立檔案的示例

在以下示例中,我們正在建立一個新檔案。建立新檔案的檔案模式將為“w”(寫入模式)。

#include <stdio.h>

int main() {
  FILE * file;

  // Creating a file
  file = fopen("file1.txt", "w");

  // Checking whether file is 
  // created or not
  if (file == NULL) {
    printf("Error in creating file");
    return 1;
  }
  printf("File created.");

  return 0;
}

輸出

File created.

開啟檔案的示例

在以下示例中,我們正在開啟一個現有檔案。開啟現有檔案的檔案模式將為“r”(只讀)。您也可以使用上面解釋的其他檔案開啟模式選項。

注意:必須存在要開啟的檔案。

#include <stdio.h>

int main() {
  FILE * file;

  // Opening a file
  file = fopen("file1.txt", "r");

  // Checking whether file is 
  // opened or not 
  if (file == NULL) {
    printf("Error in opening file");
    return 1;
  }
  printf("File opened.");

  return 0;
}

輸出

File opened.

關閉檔案

每個檔案在對其執行操作後都必須關閉。 fclose()函式 關閉開啟的檔案。

語法

以下是fclose()函式的語法:

int fclose(FILE *fp);

fclose()函式在成功時返回零,如果關閉檔案時出錯則返回EOF。

fclose()函式實際上會將緩衝區中仍掛起的所有資料重新整理到檔案,關閉檔案並釋放用於檔案的所有記憶體。EOF是標頭檔案stdio.h中定義的常量。

關閉檔案的示例

在以下示例中,我們正在關閉開啟的檔案:

#include <stdio.h>

int main() {
  FILE * file;

  // Opening a file
  file = fopen("file1.txt", "w");

  // Checking whether file is 
  // opened or not 
  if (file == NULL) {
    printf("Error in opening file");
    return 1;
  }
  printf("File opened.");

  // Closing the file
  fclose(file);
  printf("\nFile closed.");

  return 0;
}

輸出

File opened.
File closed.

寫入文字檔案

提供了以下庫函式以將資料寫入以可寫模式開啟的檔案:

  • fputc():將單個字元寫入檔案。
  • fputs():將字串寫入檔案。
  • fprintf():將格式化字串(資料)寫入檔案。

將單個字元寫入檔案

fputc()函式 是一個非格式化函式,它將引數“c”的單個字元值寫入“fp”引用的輸出流。

int fputc(int c, FILE *fp);

示例

在以下程式碼中,給定字元陣列中的一個字元被寫入以“w”模式開啟的檔案中

#include <stdio.h>

int main() {

   FILE *fp;
   char * string = "C Programming tutorial from TutorialsPoint";
   int i;
   char ch;

   fp = fopen("file1.txt", "w");

   for (i = 0; i < strlen(string); i++) {
      ch = string[i];
      if (ch == EOF)
         break;
      fputc(ch, fp);
   }
   printf ("\n");
   fclose(fp);
   
   return 0;
}

輸出

After executing the program, "file1.txt" will be created in the current folder and the string is written to it.

將字串寫入檔案

fputs()函式 將字串“s”寫入“fp”引用的輸出流。如果成功,它將返回非負值,否則在發生任何錯誤時返回EOF。

int fputs(const char *s, FILE *fp);

示例

以下程式將給定二維字元陣列中的字串寫入檔案:

#include <stdio.h>

int main() {

   FILE *fp;
   char *sub[] = {"C Programming Tutorial\n", "C++ Tutorial\n", "Python Tutorial\n", "Java Tutorial\n"};
   fp = fopen("file2.txt", "w");

   for (int i = 0; i < 4; i++) {
      fputs(sub[i], fp);
   }

   fclose(fp);
   
   return 0;
}

輸出

當程式執行時,將在當前資料夾中建立一個名為“file2.txt”的檔案,並儲存以下行:

C Programming Tutorial
C++ Tutorial
Python Tutorial
Java Tutorial

將格式化字串寫入檔案

fprintf()函式 將格式化的資料流傳送到FILE指標表示的磁碟檔案。

int fprintf(FILE *stream, const char *format [, argument, ...])

示例

在以下程式中,我們有一個名為“employee”的結構體型別陣列。該結構體具有字串、整數和浮點數元素。使用fprintf()函式,資料被寫入檔案。

#include <stdio.h>

struct employee {
   int age;
   float percent;
   char *name;
};

int main() {

   FILE *fp;
   struct employee emp[] = {
      {25, 65.5, "Ravi"}, 
      {21, 75.5, "Roshan"}, 
      {24, 60.5, "Reena"}
   };

   char *string;
   fp = fopen("file3.txt", "w");

   for (int i = 0; i < 3; i++) {
      fprintf(fp, "%d %f %s\n", emp[i].age, emp[i].percent, emp[i].name);
   }
   fclose(fp);
   
   return 0;
}

輸出

When the above program is executed, a text file is created with the name "file3.txt" that stores the employee data from the struct array.

從文字檔案讀取

以下庫函式用於讀取以讀取模式開啟的檔案中的資料:

  • fgetc():從檔案讀取單個字元。
  • fgets():從檔案讀取字串。
  • fscanf():從檔案讀取格式化字串。

從檔案讀取單個字元

fgetc() 函式從“fp”引用的輸入檔案中讀取一個字元。返回值是讀取的字元,如果發生任何錯誤,則返回 EOF。

int fgetc(FILE * fp);

示例

以下示例以逐字元的方式讀取給定檔案,直到到達檔案末尾。

#include <stdio.h>

int main(){

   FILE *fp ;
   char ch ;
   fp = fopen ("file1.txt", "r");

   while(1) {
      ch = fgetc (fp);
      if (ch == EOF)
         break;
      printf ("%c", ch);
   }
   printf ("\n");
   fclose (fp);
}

輸出

執行程式碼並檢查其輸出:

C Programming tutorial from TutorialsPoint

從檔案讀取字串

fgets() 函式從“fp”引用的輸入流中讀取最多“n - 1”個字元。它將讀取的字串複製到緩衝區“buf”中,並在字串末尾附加一個空字元以終止字串。

示例

以下程式讀取給定檔案中的每一行,直到檢測到檔案末尾:

# include <stdio.h>

int main() {

   FILE *fp;
   char *string;
   fp = fopen ("file2.txt", "r");

   while (!feof(fp)) {
      fgets(string, 256, fp);
      printf ("%s", string) ;
   }
   fclose (fp);
}

輸出

執行程式碼並檢查其輸出:

C Programming Tutorial
C++ Tutorial
Python Tutorial
Java Tutorial

從檔案讀取格式化字串

C 程式語言中的fscanf() 函式用於從檔案讀取格式化輸入。

int fscanf(FILE *stream, const char *format, ...)

示例

在以下程式中,我們使用fscanf()函式讀取不同型別變數中的格式化資料。通常使用格式說明符來指示欄位型別(%d、%f、%s 等)。

#include <stdio.h>

int main() {

   FILE *fp;
   char *s;
   int i, a;  
   float p;

   fp = fopen ("file3.txt", "r");

   if (fp == NULL) {
      puts ("Cannot open file"); return 0;
   }

   while (fscanf(fp, "%d %f %s", &a, &p, s) != EOF)
   printf ("Name: %s Age: %d Percent: %f\n", s, a, p);
   fclose(fp);

   return 0;
}

輸出

執行上述程式時,它會開啟文字檔案“file3.txt”並在螢幕上列印其內容。執行程式碼後,您將獲得如下輸出:

Name: Ravi Age: 25 Percent: 65.500000
Name: Roshan Age: 21 Percent: 75.500000
Name: Reena Age: 24 Percent: 60.500000

檔案處理二進位制讀寫函式

對於二進位制檔案,讀寫操作以二進位制形式完成。您需要在訪問模式中包含字元“b”(“wb”用於寫入二進位制檔案,“rb”用於讀取二進位制檔案)。

有兩個函式可用於二進位制輸入和輸出:fread()函式和fwrite()函式。這兩個函式都應用於讀取或寫入記憶體塊,通常是陣列或結構。

寫入二進位制檔案

fwrite() 函式將指定大小的位元組塊從緩衝區寫入以二進位制寫入模式開啟的檔案。以下是使用此函式的原型

fwrite(*buffer, size, no, FILE);

示例

在以下程式中,已聲明瞭一個名為“employee”的結構體型別的陣列。我們使用fwrite()函式寫入一個位元組塊,相當於一個employee資料的尺寸,寫入以“wb”模式開啟的檔案中。

#include <stdio.h>

struct employee {
   int age;
   float percent;
   char name[10];
};

int main() {

   FILE *fp;
   struct employee e[] = {
      {25, 65.5, "Ravi"}, 
      {21, 75.5, "Roshan"}, 
      {24, 60.5, "Reena"}
   };

   char *string;

   fp = fopen("file4.dat", "wb");
   for (int i = 0; i < 3; i++) {
      fwrite(&e[i], sizeof (struct employee), 1, fp);
   }

   fclose(fp);
   
   return 0;
}

輸出

When the above program is run, the given file will be created in the current folder. It will not show the actual data, because the file is in binary mode.

從二進位制檔案讀取

fread() 函式從以二進位制讀取模式開啟的檔案中讀取指定大小的位元組塊到指定大小的緩衝區。以下是使用此函式的原型

fread(*buffer, size, no, FILE);

示例

在以下程式中,已聲明瞭一個名為“employee”的結構體型別的陣列。我們使用fread()函式讀取一個位元組塊,相當於一個employee資料的尺寸,讀取以“rb”模式開啟的檔案中。

#include <stdio.h>

struct employee {
   int age;
   float percent;
   char name[10];
};

int main() {

   FILE *fp;
   struct employee e;

   fp = fopen ("file4.dat", "rb");

   if (fp == NULL) {
      puts ("Cannot open file"); 
      return 0;
   }

   while (fread (&e, sizeof (struct employee), 1, fp) == 1)
   printf ("Name: %s Age: %d Percent: %f\n", e.name, e.age, e.percent);

   fclose(fp);
   
   return 0;
}

輸出

執行上述程式時,它會開啟檔案“file4.dat”並在螢幕上列印其內容。執行程式碼後,您將獲得如下輸出:

Name: Ravi Age: 25 Percent: 65.500000
Name: Roshan Age: 21 Percent: 75.500000
Name: Reena Age: 24 Percent: 60.500000

重新命名檔案

rename() 函式用於將現有檔案從舊檔名重新命名為新檔名。

語法

以下是重新命名檔案的語法:

int rename(const char *old_filename, const char *new_filename)

示例

#include <stdio.h>

int main() {
  // old and new file names
  char * file_name1 = "file1.txt";
  char * file_name2 = "file2.txt";

  // Renaming old file name to new one
  if (rename(file_name1, file_name2) == 0) {
    printf("File renamed successfully.\n");
  } else {
    perror("There is an error.");
  }

  return 0;
}

輸出

如果存在檔案(file1.txt),則以下將是輸出:

Name: Ravi Age: 25 Percent: 65.500000
Name: Roshan Age: 21 Percent: 75.500000
Name: Reena Age: 24 Percent: 60.500000
廣告