C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

Cを使用して2つのファイルの内容を3番目のファイルにマージします


これは、2つのファイルの内容を3番目のファイルにマージするcプログラムです。

たとえば。

入力

java.txt is having initial content “Java is a programing language.”
kotlin.txt is having initial content “ kotlin is a programing language.”
ttpoint.txt is having initial content as blank

出力

files are merged
ttpoint.txt will have final content as “Java is a programing language. kotlin is a programing language.”

アルゴリズム

Begin
   Declare a[] array to the character datatype.
      Initialize a[] = "Java is a programing language.".
   Declare i of the integer datatype.
      Initialize i =0.
   Declare f1 as a pointer to the FILE type.
   Open a file “java.txt” to perform write operation using f1 pointer.
   while (a[i] != '\0')
      call fputc(a[i], f1) to put all data of a[] into f1 file object i++
   Close the f1 file pointer.
   Declare a[] array to the character datatype.
      Initialize b[] = " kotlin is a programing language.".
   Declare i of the integer datatype.
      Initialize j =0.
   Declare f2 as a pointer to the FILE type.
   Open a file “kotlin.txt” to perform write operation using f2 pointer.
   while (b[j] != '\0')
      call fputc(b[j], f1) to put all data of b[] into f2 file object j++
   Close the f2 file pointer.
   Open a file “java.txt” to perform read operation using f1 pointer.
   Open a file “ttpoint.txt” to perform write operation using f2 pointer.
   Declare f3 as a pointer to the FILE datatype.
   Open a file “ttpoint.txt” to perform write operation using f3 pointer.
   Declare a variable “c” to the character datatype.
   if (f1 == NULL || f2 == NULL || f3 == NULL) then
      print “couldn’t open the file.”
      Exit.
   While ((c = fgetc(f1)) != EOF) do
      Put all data of “c” variable into f3 file pointer using fputc() function.
   while ((c = fgetc(f2)) != EOF) do
      Put all data of “c” variable into f3 file pointer using fputc() function.
   Call fclose(f3) function to close the file pointer.
   Open the file ttpoint.txt using f3 file pointer.
   Print “Merged java.txt and python.txt into ttpoint.txt”
   while (!feof(f3))
      Call putchar(fgetc(f3)) function to print the content of f3 file pointer.
   Close the f1 file pointer.
   Close the f2 file pointer.
   Close the f3 file pointer.
End.

#include <stdio.h>
#include <stdlib.h>
int main() {
   char a[] = "Java is a programing language.";
   int i=0;
   FILE *f1; // Open two files to be merged
   f1 = fopen("java.txt", "w");
   while (a[i] != '\0') {
      fputc(a[i], f1);
      i++;
   }
   fclose(f1);
   char b[] = "kotlin is a programing language.";
   int j =0;
   FILE *f2;
   f2 = fopen("kotlin.txt", "w");
   while (b[j] != '\0') {
      fputc(b[j], f2);
      j++;
   }
   fclose(f2);
   f1 = fopen("java.txt", "r");
   f2 = fopen("kotlin.txt", "r");
   FILE *f3 = fopen("ttpoint.txt", "w"); // Open file to store the result
   char c;
   if (f1 == NULL || f2 == NULL || f3 == NULL) {
      puts("Could not open files");
      exit(0);
   }
   while ((c = fgetc(f1)) != EOF) // Copy contents of first file to ttpoint.txt
   fputc(c, f3);
   while ((c = fgetc(f2)) != EOF) // Copy contents of second file to ttpoint.txt
   fputc(c, f3);
   fclose(f3);
   f3 = fopen("ttpoint.txt", "r");
   printf("Merged java.txt and kotlin.txt into ttpoint.txt\n");
   while (!feof(f3))
   putchar(fgetc(f3));
   fclose(f1); //closing the file pointer.
   fclose(f2);
   fclose(f3);
   return 0;
}

出力

Merged java.txt and kotlin.txt into ttpoint.txt
Java is a programing language.kotlin is a programing language.

  1. Windows 10でWindowsファイル回復ツール(WINFR)を使用する

    Windowsファイルリカバリ (winfr.exe )は、さまざまな種類のメディア上の削除されたファイルを回復するための新しい無料のMicrosoftツールです。このツールは、Windows 10 2004(2020年5月の更新)以降で使用可能であり、コマンドプロンプトのコンソールモードでのみ機能します。 WINFRは、MicrosoftストアからWindows10にインストールできます。このツールを使用すると、ファイルシステムをスキャンして、削除されたファイルを見つけることができます。いくつかのスキャンモードがサポートされています(MFTテーブル、既知のファイルタイプシグニチャまたはセグメ

  2. CMD を使用して Excel ファイルを 1 つにマージする方法 (4 ステップ)

    Excel を結合する方法を探している場合 CMD を使用してファイルを 1 つに 、あなたは正しい場所にいます。さまざまな Excel ファイルのデータを 1 つに結合する必要がある場合があります。これをすばやく行うには、 CMD でいくつかのコマンド ラインを適用するだけです。 または コマンド プロンプト . それでは、本編から始めましょう。 ワークブックをダウンロード CMD を使用して Excel ファイルを 1 つにマージする 4 つのステップ ここでは、次の 3 つのファイル January.xlsx があります。 、February.xlsx 、および March.x