Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 18-02-2010
Member
 
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.
Reply With Quote
  #2  
Old 18-02-2010
Member
 
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
Reply With Quote
  #3  
Old 18-02-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
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;
}
Reply With Quote
  #4  
Old 18-02-2010
Member
 
Join Date: May 2008
Posts: 2,383
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; 
        }
        
    } 

}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 18-02-2010
Member
 
Join Date: Feb 2008
Posts: 1,848
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;
	}
 
 
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to read 2 text files and write it into one"
Thread Thread Starter Forum Replies Last Post
Need to read input text and write it to output file Tramba-keshwar Software Development 6 26-06-2011 10:38 PM
Is it possible to use one file handle to read and write in different files in c++? KALANI84 Software Development 5 02-02-2010 04:20 PM
How to read and write files in Java BansiJ Software Development 3 02-09-2009 08:52 PM
How to read and write Excel files in PHP ? Sudra Software Development 3 06-05-2009 05:18 PM
Download Free Software to Convert Excel XLS to HTML and Read or Write XLTM, XLMX files Maximus() Windows Software 0 29-01-2009 06:31 PM


All times are GMT +5.5. The time now is 06:30 PM.