C ファイル処理の基本

PHPz
リリース: 2023-09-16 08:29:02
転載
1332 人が閲覧しました

C 文件处理基础知识

ここでは、C 言語での基本的なファイル処理操作をいくつか見ていきます。これらの操作のリストは次のとおりです。

    #ファイルへの書き込み
  • ファイルからの読み取り
  • ファイルへの追加
コンテンツをファイルに書き込む

コンテンツをファイルに書き込む方法を理解するには、次のコードを参照してください。

サンプル コード

#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;
}
ログイン後にコピー

出力

sample.txt: opened in write mode.
sample.txt: closed successfully.
ログイン後にコピー

2.ファイルからの読み取り

コードを表示して、ファイルからの読み取り方法を理解します。 ファイル (file_read.txt) を作成します:

C プログラミング言語を使用して、読み取り専用モードでファイルを開きました。

サンプル コード

#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;
}
ログイン後にコピー

出力

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.内容をファイルに追加

コードを表示して、ファイルに行を追加する方法を確認します。

ファイル(file_append.txt)の作成

This text was already there in the file.
ログイン後にコピー

サンプルコード

#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;
}
ログイン後にコピー

出力

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.
ログイン後にコピー

以上がC ファイル処理の基本の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:tutorialspoint.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!