I'm having problems working with .txt files in C++.. In the C++ main function the name of an input file (file with ending .txt) shall be inputted, the name of an output file (file with ending .txt) as well as the desired width of the output block column format. Following in a loop read the input file character by character, most senseful at least as many characters as specified by the block width. Then compute all needed information and output the newly formatted output text line into the output file. Count for example the number of read words, characters and white spaces of the newly to print text line. Calculate the number of needed spaces in between the single words of a line and insert - except before the last word - same number of spaces in between the words (i.e. don't fill all missing spaces in front of the last word of a line (see examples). Since the input text might be arbitrary long and especially longer than bytes in the main memory exist do NOT read first the whole complete input text into a single array or a list of characters but work (output) line by line!
#include <iostream>
#include <fstream>
#include <string>
#include<iomanip>
using namespace std;
int main(void)
{ char c;
ifstream myfile;
string fileLocation;
cout << "Please enter the location of the file: ";
getline(cin, fileLocation);
myfile.open(fileLocation.c_str());
if (!myfile.is_open())
{
cerr<<"error opening file"<< fileLocation<< endl;
}
else {
cout<< "input file " << fileLocation<< "opened.." << endl;
while(!myfile.eof())
{
c = myfile.get();
cout << setw(40) ;
cout.put(c);
}
myfile.close();
cout<<"\n input file closed" << endl;
}
return 0;
Aucun commentaire:
Enregistrer un commentaire