If the intention is to read line by line a text file filtering the contents of each line by removing double spaces by single spaces, follow 2 solutions in C++ using the 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;
}
Inverse would be from start (line 1, index 0) to end (line x, index y)?
– CypherPotato
Take a look here.
– CypherPotato
@Cypherpotato yes.
– lucasbento
What is the purpose of reading the byte-to-byte string? A filter that removes duplicate whitespace ?
– Lacobus
@Lacobus, the intention is to remove excesses of spaces, leaving only one space between each word, beginning and end of the line.
– lucasbento
@Lacobus I must create a new question for that purpose?
– lucasbento