Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Asas pemprosesan fail C

Asas pemprosesan fail C

PHPz
PHPzke hadapan
2023-09-16 08:29:021395semak imbas

C 文件处理基础知识

Di sini kita akan melihat beberapa operasi pemprosesan fail asas dalam bahasa C. Berikut ialah senarai operasi ini:

  • Tulis ke fail
  • Baca dari fail
  • Tambahkan ke fail

Tulis ke fail

Sila rujuk kod berikut untuk memahami cara Menulis ke fail

#include <stdio.h>
int main() {
   FILE *fp;
   char *filename = "sample.txt";
   char *content = "Hey there! You&#39;ve successfully created a file with content in c programming language.";
   /* open for writing */
   fp = fopen(filename, "w");
   if( fp == NULL ) {
      printf("%s: failed to open. </p><p>", filename);
      return -1;
   } else {
      printf("%s: opened in write mode.</p><p>", filename);
   }
   /* Write content to file */
   fprintf(fp, "%s</p><p>", content);
   if( !fclose(fp) )
      printf("%s: closed successfully.</p><p>", filename);
   return 0;
}

Output

sample.txt: opened in write mode.
sample.txt: closed successfully.

2. Membaca dari fail

Lihat kod untuk memahami cara kita membaca daripada fail Cipta fail (file_read.txt):

Anda membuka fail dalam mod baca sahaja menggunakan bahasa pengaturcaraan C.

Contoh Kod

#include <stdio.h>
int main() {
   FILE *fp;
   char *filename = "file_read.txt";
   char ch;
   /* open for writing */
   fp = fopen(filename, "r");
   if (fp == NULL) {
      printf("%s does not exists </p><p>", filename);
      return;
   } else {
      printf("%s: opened in read mode.</p><p></p><p>", filename);
   }
   while ((ch = fgetc(fp) )!= EOF) {
      printf ("%c", ch);
   }
   if (!fclose(fp))
      printf("</p><p>%s: closed.</p><p>", filename);
   return 0;
}

Output

file_read.txt: opened in read mode.
You have opened a file using C programming language, in read-only mode.
file_read.txt: closed.

3.Tambah kandungan pada fail

Lihat kod untuk melihat cara menambahkan baris pada fail.

Buat fail (file_append.txt)

This text was already there in the file.

Contoh kod

#include <stdio.h>
int main() {
   FILE *fp;
   char ch;
   char *filename = "file_append.txt";
   char *content = "This text is appeneded later to the file, using C programming.";
   /* open for writing */
   fp = fopen(filename, "r");
   printf("</p><p>Contents of %s -</p><p></p><p>", filename);
   while ((ch = fgetc(fp) )!= EOF) {
      printf ("%c", ch);
   }
   fclose(fp);
   fp = fopen(filename, "a");
   /* Write content to file */
   fprintf(fp, "%s</p><p>", content);
   fclose(fp);
   fp = fopen(filename, "r");
   printf("</p><p>Contents of %s -</p><p>", filename);
   while ((ch = fgetc(fp) )!= EOF) {
      printf ("%c", ch);
   }
   fclose(fp);
   return 0;
}

Output

Contents of file_append.txt -
This text was already there in the file.
Appending content to file_append.txt...
Content of file_append.txt after &#39;append&#39; operation is -
This text was already there in the file.
This text is appeneded later to the file, using C programming.

Atas ialah kandungan terperinci Asas pemprosesan fail C. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:tutorialspoint.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam
Artikel sebelumnya:penimbal blokArtikel seterusnya:penimbal blok