Change improper whitespace to proper

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;

string removeWhitespace(const string & s);

int getbaddata()
{
	int x = 1;
	while (x == 1)
	{
		string input;
		ofstream file("fixedtext.txt");
		cout << "Enter words with too many spaces in between or 0 to end: " << endl;
		getline(cin,input,'\n');
		file <<  removeWhitespace(input) << endl;
		ofstream file.close;
	}
	return 0;
}

string removeWhitespace(const string & s)
{
    istringstream iss(s);
    string result;
    string temp;
	
    while (iss>>temp)
    {
        result+=temp;
        result+=' ';
    }
	
    result.erase(result.end()-1);
	
    return result;
}
int showcleandata()
{
	ifstream("fixedtext.txt");
	return 0;
}
int main()
{
	getbaddata();
}
Last edited on
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.
Hint: the std::unique() algorithm (#include <algorithm>) can help you. Depending on what you really want, other algorithms may also apply:
http://www.cplusplus.com/reference/algorithm/unique/

Hope this helps.

[edit] Fixed typo
Last edited on
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!
1
2
3
4
#include <algorithm>
#include <cctype>
#incude <iostream>
#include <string> 
1
2
3
4
bool is_both_space( char a, char b )
  {
  return std::isspace( a ) && std::isspace( b );
  }
1
2
3
std::string s = "  Hello    world!   ";  // or use: getline( cin, s ); ...
s.erase( std::unique( s.begin(), s.end(), is_both_space ), s.end() );
cout << s << endl;  // prints " Hello world! " 

Notice how all space sequences are now just a single space.

Hope this helps.
/* 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>
}

}


int main()
{
getbaddata();
showcleandata();

return 0;
}
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
Topic archived. No new replies allowed.