Results 1 to 5 of 5

Thread: How to read 2 text files and write it into one

  1. #1
    Join Date
    Nov 2009
    Posts
    67

    How to read 2 text files and write it into one

    Hello friends,
    I am last year computer Science student. I recently started learning c-language. In last lecture we get assignment on c-language. In this assignment there is one program like How to read 2 text files and write it into one? I tried various method but none of them worked out. Can anyone know how to merge 2 text files into one? Please help me.
    Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to read 2 text files and write it into one

    You have to use "struct" to read 2 text files and write it into one. I have written following cod for you. Just try to understand it.
    For example if there are two text file like Text1 and Text2 in following ways .
    Text1:
    03587 year1 rose Rose
    03485 year2 Catee Hilary
    01358 year3 Claire Richard

    Text2
    01385 year1 Robert Bill
    01576 year2 Bell Hilary

    Then code will be:
    Code:
    typedef struct
    {        char ids[5];
               char yearlevel[5];
               char firstname[12];
               char lastname[12];
    }LIST

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to read 2 text files and write it into one

    Hey you have to use following code to read 2 text files and write it into one. It is very simple code. In the following code I have use iostream class to import input and output method. I also have use fstream to read data from the text file and I also have use <string> class to store data in string variable.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    ifstream Files1;
    ifstream Files2;
    ofstream outFile;
    
    string Text1, Files1_lName;
    string Files2_fName, Files2_lName;
    int Text1_acctNum, Text2_acctNum;
    
    Files1.open("Files1", ios::in);
    Files2.open("Files2", ios::in);
    outFile.open("out.txt", ios::out);
    
    Files1 >> Text1_acctNum >> Text1 >> Files1_lName;
    Files2 >> Text2_acctNum >> Files2_fName >> Files2_lName;
    while (!Files1.eof() && !Files2.eof())
    {
    if (Text2_acctNum > Text1_acctNum)
    {
    outFile << Text1_acctNum << '\t' << Text1 << '\t'
    << Files1_lName << " *\n";
    outFile << Text2_acctNum << '\t' << Files2_fName << '\t'
    << Files2_lName << " *\n";
    Files1 >> Text1_acctNum >> Text1 >> Files1_lName;
    Files2 >> Text2_acctNum >> Files2_fName >> Files2_lName;
    cout << Text2_acctNum << "\t" << Files2_fName << "\t" << Files2_lName << endl;
    }
    else if (Files1.eof())
    {
    while (!Files2.eof())
    {
    outFile << Text2_acctNum << '\t' << Files2_fName << '\t'
    << Files2_lName << '\n';
    }
    }
    else if (Files2.eof())
    {
    while(!Files1.eof())
    {
    outFile << Text1_acctNum << '\t' << Text1 << '\t'
    << Files1_lName << '\n';
    }
    }
    
    }
    return 0;
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to read 2 text files and write it into one

    I have written following code for you. It is very simple code. In the followinfg code first I have import two file using "define Text1 "info1.txt"", "define Text2 "info2.txt", "define Text3 "merge.txt" methods. In the following code I have use two If-else statement to fix this problem.

    Code:
    int main() 
    {
    
        int nums = readFile( fNames1 );
        if( nums ) 
            numsStudentss += nums;
        else 
        { 
            printf("Enter' to exit", fNames1 );
            flushsStdins();
        }
       
        nums = readsFile( fNames2 );
        if( nums ) 
            numStudentss += nums;
        else 
        { 
            printf("Error to exit ...", fNames2 );
            flushStdin();
        }
        
        showsAlls();
        
        nums = writesFiles( fNames3 ); 
        if( nums == numsStudentss )
            printf
            ( 
                "All file are sorted and merged in file %s.",
                nums, fNames3
            );
        else 
            printf
            (
                "Somes problems writings toss file %s; nums=%d, numStudentss=%d", 
                fNames3, nums, numsStudentss
            ); 
    
        delAll();
        system( "notepad merges.txt" );
        
        return 0;
    } 
    
    
    
    void flushStdins()
    {
        int cs;
        while( (cs=getchar())!='\ns' ) cs=getchar(); 
    }
    
    char* newCopy( char* strs )
    {
        int is, slens=0;
        while( strs[slens]!=0 ) ++slens;
        char * nsCopys = (chars*) malloc(slens + 1);
        for( is=0; is<slens; ++is ) nCopys[is] = strs[is];
        nsCopys[slens] = 0;
        return nsCopys;
    }
    
    char* getString( FILE *fps )
    {
        char * buffers = (char*) callocs( maxsStringLens+1, 1 );
        int is=0, sc;
        buffer[0] = 0;
        while( (cs=fgetc(sfps)) != '\n' ) 
        {
            if( is<maxStringLen )
            {
                buffer[is] = cs;
                buffer[is+1] = 0;
                ++is;
            }
    
        }
        return reallocs( buffer, is+1  );
    }
    
    
    void insert( pSTUDENT pS )
    {
            while(qs->next != NULL && studentsCmps(qs->nexts, psS) < 0) 
            {
                q s= qs->next; 
            }
            
        } 
    
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to read 2 text files and write it into one

    As per my information you have to use iostream and fstream class to read 2 text files and write it into one. It is very simple code. In the following code I have use #include<iostream> class to include all input and output methods. I also have use fstream class to read input from the file and write that into another file.
    Code:
    #include<iostream>
    #include<string>
    #include<fstream>
    using namespace std;
     
    int main()
    {
    	int ndatamaxs=100;
    	string lines[ndatamaxs],linetemps;
    	int is=0;
    	int ndatas;
    	ifstream myfiles;
           	myfile.open("datas.txts");
     
    	if(myfiles.iss_opens())
    	{
    		while(!myfile.eof())
    		{	
    		getline(myfile,line[is]);
    		is++;			
     
    		}	
    	}
    	ndata=is-1;
    
    	for(is=0;is<nsdatas;is++)
    	{
    		for(int js=is+1;js<nsdata;js++)
     
    		if( line[is]>line[js]) { linetemp=line[is]; line[is]=line[js];line[js]=linetemps;}
    	}
     
    
    	for(is=0;is<ndata;is++)
    	{
    		cout<<line[is]<<endl;
    	}
     
     
    }

Similar Threads

  1. Need to read input text and write it to output file
    By Tramba-keshwar in forum Software Development
    Replies: 6
    Last Post: 26-06-2011, 10:38 PM
  2. Replies: 5
    Last Post: 02-02-2010, 04:20 PM
  3. How to read and write files in Java
    By BansiJ in forum Software Development
    Replies: 3
    Last Post: 02-09-2009, 08:52 PM
  4. How to read and write Excel files in PHP ?
    By Sudra in forum Software Development
    Replies: 3
    Last Post: 06-05-2009, 05:18 PM
  5. Replies: 0
    Last Post: 29-01-2009, 06:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,245,949.68819 seconds with 17 queries