If the intention is to read line by line of a text file by filtering the contents of each line with the removal of double spaces by single spaces, follow 2 C ++ solutions using STL
:
Using the functions std::unique()
and std::string::erase()
:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
bool check_double_spaces( char a, char b) { return ((a == b) && (a == ' ')); }
int main()
{
string linha;
ifstream entrada("arquivo.txt");
while( getline( entrada, linha) )
{
string::iterator end = unique( linha.begin(), linha.end(), check_double_spaces );
linha.erase( end, linha.end() );
cout << linha << endl;
}
return 0;
}
Using the functions std::string::find()
and std::string::replace()
:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string linha;
ifstream entrada("arquivo.txt");
size_t idx;
while( getline( entrada, linha ) )
{
while( (idx = linha.find(" ")) != string::npos ) // Dois espaços
linha.replace( idx, 2, " " ); // Um espaço
cout << linha << endl;
}
return 0;
}