Thursday, July 9, 2009

C++ Help- Input and Output txt document?

Please can someone help me with my c++ program. I need to read a file, display it on the screen, and then write the data to an output file called output.txt. Each functionality should be defined in a class, leaving the main function clean. This is what i have so far.





#include%26lt;iostream%26gt;


#include%26lt;fstream%26gt;


#include %26lt;string%26gt;


using namespace std;





void gettext();


void output();





int main(){


void gettext(string i);


void output();


return 0;


}





void gettext(string i){





string line;


ifstream myfile("input.txt");


if(myfile.is_open()){


while(!myfile.eof()){


getline(myfile, line);


cout%26lt;%26lt; line%26lt;%26lt; endl;


}


}


else{


cout%26lt;%26lt;"Problem opening file"%26lt;%26lt; endl;


}


}


void output(){


string line;





ofstream my2file;


my2file.open ("output.txt");


my2file%26lt;%26lt; line;


my2file.close();


}

C++ Help- Input and Output txt document?
It seems like you overlooked the part of the problem statement that requires you to create a class. You need something like this:





class FileHandler {


public:


FileHandler() : { } // Default constructor


FileHandler(const string%26amp;) ; // Constructor (in file only)


FileHandler(const string%26amp;, const string%26amp;); // Full constructor (in and out filenames)


FileHandler(const FileHandler%26amp;); // Copy constructor


~FileHandler() { } // Destructor


void setInFileName(const string%26amp;);


void readInFile();


void setOutFileName(const string%26amp;);


void writeOutFile(const string%26amp;);


private:


string inFileName;


string outFileName;


vector%26lt;string%26gt; inFileText;


};





In main(), then you could:





// get input and output file names; maybe they're in argv[1]


// and argv[2], or maybe you prompt for them; store the names


// in string inFileName and string outFileName





FileHandler myFileHandler = new FileHandler(inFileName);


myFileHandler-%26gt;readInFile();


myFileHandler-%26gt;writeOutFile(outFileNam...


...


// when you're done :


delete myFileHandler;





You have some problems with the code you've written. In main, you declare gettext and output, after you declared them prior to main. Also, the gettext declaration in main is not the same as the previous one, and you are not calling any functions. You don't want these anyway, the read and write operations need to be in the class.





Your input() function is just displaying the input file, not saving the contents. If it saves the file contents, you'll be able to create the output file later.





Your output() function is writing the contents of a default-constructed string, your 'line' variable', to output.txt. Certainly not what you want.





Obviously I'm leaving a lot of the details for you to fill in. I hope the class definition will get you started on solving the problem as stated.
Reply:You've got to put in classes then. If you're have an input function in your first class... it will need to accept your ifstream object. Same for your output function. Then, when calling your function pass in your input and output files respectively.
Reply:This program takes the name of a file from the keyboard (without error-checking) opens it, gets the file length, and copies that many characters both to the screen and to the file Output.txt.





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;





using namespace std;





int main()





{





char Name[30], c, length;


ifstream Myfile;


ofstream Outputfile;


cout %26lt;%26lt; "Enter the name of the file to open:";


cin %26gt;%26gt; Name;


Myfile.open(Name);


Outputfile.open("Output.txt");


Myfile.seekg(0,ios::end);


length=Myfile.tellg();


Myfile.seekg(0,ios::beg);


for (int i=0;i%26lt;length;i++)


{Myfile %26gt;%26gt; c;


cout %26lt;%26lt; c;


Outputfile %26lt;%26lt; c;


}


Myfile.close();


Outputfile.close();


return 0;


}











Output is:


jplatt@darkstar:~/Apr06$ ./filefun


Enter the name of the file to open:myfile.txt


IhateC++\!!!!jplatt@darkstar:~/Apr06$ cat Output.txt


IhateC++\!!!!jplatt@darkstar:~/Apr06$ cat myfile.txt


I hate C++\!





Have fun rewriting it to your purposes -- oh, and in case you haven't figured it out yet, it's compiled with GCC for Linux. Your OS and compiler may handle it slightly differently.


No comments:

Post a Comment