943,824 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1273
  • C++ RSS
Apr 1st, 2008
0

how to read many text file?

Expand Post »
I have this to file.First

ayam.txt

10 20 30 60 90 50 40

and itik.txt

a b c d e f g h i


How to call read and write the ayam.txt and itik.txt into one file.

which first i would like to cout the ayam.txt and then itik.txt.
Similar Threads
Reputation Points: 9
Solved Threads: 0
Posting Whiz in Training
nurulshidanoni is offline Offline
219 posts
since Nov 2007
Apr 1st, 2008
0

Re: how to read many text file?

U use ifstream and ofstream that is to read u use ifstream and to write u use ofstream..... assuming that u have declared the ofstream and ifstream files but to make a long story short....

U have to declare ur ifstream and ofstream files.... u can achieve that by doing the ff ifstream in_file; and ofstream out_file; and then U need to open them that is reading and writting using in_file.open("eyam.txt"); and out_file.open("eyam.txt"); u use ifstream variable to read and ofstream's to write so U will have something like out_file << number; whatever u wana write and use in_file to read whatever u wana read
Reputation Points: 26
Solved Threads: 19
Posting Whiz in Training
Traicey is offline Offline
283 posts
since Mar 2008
Apr 1st, 2008
0

Re: how to read many text file?

I have this to file.First

ayam.txt

10 20 30 60 90 50 40

and itik.txt

a b c d e f g h i

How to call read and write the ayam.txt and itik.txt into one file.

which first i would like to cout the ayam.txt and then itik.txt.
#include <iostream.h>
#include <string.h>
#include <fstream.h>

ofstream output_file("out.4", ios::app);

class MERGE_FILE{

   public:      // public interfaces for this class

        MERGE_FILE(char *, int);	
        MERGE_FILE operator + (MERGE_FILE &); 


   private: // private var to be used by this class only 

	char inputs[20][10];  // char array to hold file inputs
	char in_file[10]; 	   // array to hold input file name

}; //END OF CLASS INTERFACE


MERGE_FILE::MERGE_FILE(char * i_f, int x)
{
	ifstream input_file(in_file, ios::in);
	int i;
	if (strcmp(i_f, "empty")==0)
	{
		strcpy(in_file,"No input file");
	}
	else if (strcmp(i_f, "empty")!=0)
	{
		n=x; //number of inputs to read
                strcpy(in_file,i_f);
		for (i=0;i<n;i++)
		{
                        input_file>> inputs[i];
		}
		output_file<<"INPUT FILE NAME IS "<<i_f<<"."<<endl;
		output_file<<"Read  "<<n<<" inputs "<<endl;
	}
	else {}	 
}

MERGE_FILE 
MERGE_FILE::operator + (MERGE_FILE & other_obj)
{

	int i;
        MERGE_FILE temp_obj("empty",0);
        temp_obj.n =n + other_obj;
        for (i=0; i<n; i++)
        {
                 temp_obj.intputs= inputs[i];
        }
        for(i=0;i<other_obj.n;i++)
        {
                temp_obj.inputs[i+n]=other_obj.inputs[i];
        }
        output_file<<" Theres "<< n<< " inputs in first file"<<endl;
        output_file<<" Theres "<<other_obj.n<< " inputs in second file"<<endl;
        output_file<<" Total inputs equal "<<temp_obj.n<<endl;
        return temp_obj;
}
now all you need to do is save as an header file and initiate this class in your main file and it should merge your files, haven't tested but it's a start. so you work the bugs out
Last edited by scarface3288; Apr 1st, 2008 at 11:40 am.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
scarface3288 is offline Offline
14 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: array of strings
Next Thread in C++ Forum Timeline: Help with C++ PLEASE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC