DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   how to read many text file? (http://www.daniweb.com/forums/thread116706.html)

nurulshidanoni Apr 1st, 2008 2:43 am
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.

Traicey Apr 1st, 2008 10:11 am
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

scarface3288 Apr 1st, 2008 11:38 am
Re: how to read many text file?
 
Quote:

Originally Posted by nurulshidanoni (Post 573808)
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


All times are GMT -4. The time now is 11:14 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC