{"id":3634,"date":"2024-03-06T11:38:56","date_gmt":"2024-03-06T11:38:56","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3634"},"modified":"2024-03-06T11:38:56","modified_gmt":"2024-03-06T11:38:56","slug":"c-program-to-merge-contents-of-two-files-into-a-third-file","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/c-program-to-merge-contents-of-two-files-into-a-third-file\/","title":{"rendered":"C Program To Merge Contents Of Two Files Into A Third File"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#c-program-to-merge-contents-of-two-files-into-a-third-file\">C Program To Merge Contents Of Two Files Into A Third File<\/a><\/li><li ><a href=\"#faq-c-program-to-merge-contents-of-two-files-into-a-third-file\">FAQ- C Program To Merge Contents Of Two Files Into A Third File<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-program-to-merge-contents-of-two-files-into-a-third-file\">C Program To Merge Contents Of Two Files Into A Third File<\/h2>\n\n\n\n<p>The given steps outline a simple process for merging the contents of two files, <code>file1.txt<\/code> and <code>file2.txt<\/code>, into a new file named <code>file3.txt<\/code>. <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Open <code>file1.txt<\/code> and <code>file2.txt<\/code> in read mode.<\/li>\n\n\n\n<li>Open <code>file3.txt<\/code> in write mode.<\/li>\n\n\n\n<li>Copy characters from <code>file1.txt<\/code> to <code>file3.txt<\/code> in a loop.<\/li>\n\n\n\n<li>Copy characters from <code>file2.txt<\/code> to <code>file3.txt<\/code> in a loop.<\/li>\n\n\n\n<li>Close all files.<\/li>\n<\/ol>\n\n\n\n<p>These steps describe a basic file merging operation where the contents of <code>file1.txt<\/code> and <code>file2.txt<\/code> are sequentially copied to a new file, <code>file3.txt<\/code>. This process can be implemented in various programming languages, such as C, C++, Python, etc.<\/p>\n\n\n\n<p><strong>Inorder to run the below program successfully, we have to include the file1.txt and fil2.txt must exits in the same folder<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n#include &lt;stdio.h&gt; \n#include &lt;stdlib.h&gt; \n  \nint main() \n{ \n   \/\/ Open two files to be merged \n   FILE *fp1 = fopen(\"file1.txt\", \"r\"); \n   FILE *fp2 = fopen(\"file2.txt\", \"r\"); \n  \n   \/\/ Open file to store the result \n   FILE *fp3 = fopen(\"file3.txt\", \"w\"); \n   char c; \n  \n   if (fp1 == NULL || fp2 == NULL || fp3 == NULL) \n   { \n         puts(\"Could not open files\"); \n         exit(0); \n   } \n  \n   \/\/ Copy contents of first file to file3.txt \n   while ((c = fgetc(fp1)) != EOF) \n      fputc(c, fp3); \n  \/\/ Copy contents of second file to file3.txt \n   while ((c = fgetc(fp2)) != EOF) \n      fputc(c, fp3); \n  \n   printf(\"Merged file1.txt and file2.txt into file3.txt\"); \n  \n   fclose(fp1); \n   fclose(fp2); \n   fclose(fp3); \n   return 0; \n} <\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Merged file1.txt and file2.txt into file3.txt\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-c-program-to-merge-contents-of-two-files-into-a-third-file\">FAQ- C Program To Merge Contents Of Two Files Into A Third File<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1700480892929\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. How to add contents of one file to another in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <br \/>Open <code>file1.txt<\/code> and <code>file2.txt<\/code> in read mode.<br \/>Open <code>file3.txt<\/code> in write mode.<br \/>Copy characters from <code>file1.txt<\/code> to <code>file3.txt<\/code> with a newline for readability.<br \/>Copy characters from <code>file2.txt<\/code> to <code>file3.txt<\/code> with a newline.<br \/>Display the contents of <code>file2.txt<\/code> to the console (stdout).<br \/>Close all files.<br \/>This process involves merging the contents of <code>file1.txt<\/code> and <code>file2.txt<\/code> into a new file <code>file3.txt<\/code> while enhancing readability by explicitly writing newline characters between the content from each source file. Additionally, it displays the contents of <code>file2.txt<\/code> to the console.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700480898247\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. How can I combine or concatenate contents of two files and store into a third file in Linux?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The <code>cat<\/code> command in Linux is a handy tool for files. It shows what&#8217;s inside a file (<code>cat filename<\/code>), helps make a new file (<code>cat > newfile<\/code>), adds stuff to an existing file (<code>cat >> existingfile<\/code>), and combines info from different files (<code>cat file1 file2 > combinedfile<\/code>). It&#8217;s a useful and common command in Linux.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1700480933721\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.Can you include a .C file into another .C file?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Yes, it is possible to  include a C file in another C file,<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>C Program To Merge Contents Of Two Files Into A Third File The given steps outline a simple process for merging the contents of two files, file1.txt and file2.txt, into a new file named file3.txt. These steps describe a basic file merging operation where the contents of file1.txt and file2.txt are sequentially copied to a &#8230; <a title=\"C Program To Merge Contents Of Two Files Into A Third File\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/c-program-to-merge-contents-of-two-files-into-a-third-file\/\" aria-label=\"More on C Program To Merge Contents Of Two Files Into A Third File\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":3636,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[597],"class_list":["post-3634","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-program-to-merge-contents-of-two-files-into-a-third-file","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=3634"}],"version-history":[{"count":6,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3634\/revisions"}],"predecessor-version":[{"id":7952,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3634\/revisions\/7952"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/3636"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}