Thread: merge text files in C

  1. #1
    Registered User
    Join Date
    Jun 2021
    Posts
    2

    merge text files in C

    Hi I am trying to create a program in which I want to store the words which I have from one text file to another text file but I do not want to delete the elements in which I have to the second file .

    The problem is that when I ran my code and it overwrites elements from one file to another instead of "passing" elements from one file to another .

    How can I fix this? Is it better to store to a third empty file?
    Excuse for my english

    text files :
    con1 (github.com)
    con2 (github.com)

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    int main ()
    {
        
        FILE *fp1, *fp2;
        char arr[10][100]={"aaaa","bbbbbbdbdhcdc","bjjbkgfjnfjbfj","vfvmvldvddkv"};
        fp1=fopen("con1.txt","w");
        int i=0;
        for(i=0 ; i<4 ; i++)
        {
            fputs(arr[i],fp1);
            fputs(" ",fp1);
        }
        
        fclose(fp1);
        
        
        fp1=fopen("con1.txt","r");
        
        
        fp2=fopen("con2.txt","w");
        
        
        fseek(fp2,0,SEEK_END);
        
        
        for(i=0 ; i < 4 ; i++)
        {
            fputs(" ",fp2);
            fputs(arr[i],fp2);
        }
        
        fclose(fp1);
        fclose(fp2);
        
        
    return 0;    
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,611
    Do you realize that the "w" open mode erases all the contents of the file?

    Perhaps you should consider using one of the "append" modes instead?

    See this link for more information.

  3. #3
    Registered User
    Join Date
    Jun 2021
    Posts
    2
    @jimblumberg Thank you !!!I want to be honest I have not ever used "w+" ,"r+" ,"a" until now
    I can edit my program if you want?

  4. #4
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    339

    Lightbulb

    Perhaps you should consider using one of the "append" modes instead?
    Code:
    fp2 = fopen( "con2.txt", "a" );

    "without goto we would be wtf'd"

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    Quote Originally Posted by vg-png View Post
    merge text files in C
    Did you mean "Merge" the two files or "Append" them? A Merge usually implies some sort of Alphanumeric insertion.

    For data safety, especially during testing, I would recommend copying data from both files to a third file.

    Given two source files:
    Code:
    File 1             File 2
    
    Apple              Banana
    Carrot             Doughnut
    Eclair             Fries
    A "Merge" into a third file:
    Code:
    File 3
    
    Apple
    Banana
    Carrot
    Doughnut
    Eclair
    Fries
    An "Append" to a third file:
    Code:
    File 3
    
    Apple
    Carrot
    Eclair
    Banana
    Doughnut
    Fries

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by vg-png View Post
    Hi I am trying to create a program in which I want to store the words which I have from one text file to another text file but I do not want to delete the elements in which I have to the second file .
    The way I would do it is to take a list of filenames on the commandline, open them, and write the contents to stdout.
    Basically the Unix "cat" command.
    User can then do what he wants with the output

    It's very easy to write a loop that puts characters to stdout until it hits EOF..
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MAC C Scanning text documents, merge and summing lines
    By deon0783 in forum C Programming
    Replies: 3
    Last Post: 09-14-2017, 11:05 PM
  2. Merge Binary Files
    By chinook86 in forum C Programming
    Replies: 7
    Last Post: 01-21-2008, 02:19 PM
  3. Replies: 3
    Last Post: 05-05-2004, 05:40 PM

Tags for this Thread