I am working on a program that can write a text file from improperly formatted text and then show the contents of that file. The text has too many spaces between words, and I need to remove them preserving the line breaks. The code I have here can make a text file, but cannot write to it. I appreciate any help I can get. I also am trying to get the getbaddata input to loop so that users can enter multiple lines, but am having no luck. Thanks for any help you can provide!
The >> operator of istream ignores whitespaces, so removing extra whitespace while preserving line breaks is no big deal if you use stringstreams and the getline method of ifstream.
Thanks for the suggestions. I am almost new to C++, so I do not know how to adapt the program according to the responses you posted. If you could give an example of how to implement the solution that would be great!
/* This very basic program will show you how to open a file for writing, passing as many lines as you like to the file.
* and how to open a file for reading. There are several problems with this program however. In that if you type an incorrect
* filename to the showcleandata function the program exits. In real life it should have a retry option for people who
* mistype the filename. However the program as it stands will give you a push in the right direction.
*
* cheers mikeofthenight
*/
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <cstdlib> // added
#include <temp.hpp>
using namespace std;
void getbaddata()
{
cout << "Enter name of file to save modified text to: ";
string FileName;
cin >> FileName; // get file you wish to save data to.
ofstream fout(FileName.c_str(), ios::out | ios::app);
// open file for appending data to.
if (fout.is_open() == false){
cerr << "Unable to open " << FileName << endl;
exit(EXIT_FAILURE); // defined <cstdlib>
}
cin.get(); // clear buffer
cout << "Enter words with too much whitespace (enter a blank line to quit):n";
string line,name;
while (getline(cin,line) && line.size() > 0) {
istringstream istr(line); // use a stream to discard whitespace
while (istr >> line) {
name.append(line) += " "; // build up the line of data with one space char
}
fout << name << endl; // write it to the file
name.clear(); // clear the string ready for next line of data
}
fout.close(); // finished so close the file
}
void showcleandata()
{
char ch;
cout << "Enter name of file to display: ";
string FileName;
cin >> FileName; //
ifstream fin(FileName.c_str(), ios::in);
if (fin.is_open()) {
cout << "Here are the new contents of the "
<< FileName.c_str() << " file:n";
while (fin.get(ch)) // get each char in the file
cout << ch; // and display it to stdout
fin.close();
} else {
cerr << "Unable to open " << FileName
<< " for reading!" << endl;
exit(EXIT_FAILURE); // defined <cstdlib>
}
In my program that I sent you is the line #include <temp.hpp> Please delete this from the file as it is
part of my debugging program and has nothing to do with the running of the program.
Sorry about that. mikeofthenight