How to read file line . txt from index 0

Asked

Viewed 1,589 times

0

I have the following excerpt of code that reads line by line from a text file, ending until the beginning of string. How should I change it, so that such action occurs in reverse?

while (getline (entrada, linha)){
    int tam_linha = linha.size(); /* Tamaho da linha, caracateres por linha */

    for(int i=tam_linha-1; i>=0; i--)
    {
        if(linha[i] == ' ' && linha[i-1] == linha[i])
        {
            linha.erase(linha.begin()+i);                    
        }
    }
}
  • Inverse would be from start (line 1, index 0) to end (line x, index y)?

  • Take a look here.

  • @Cypherpotato yes.

  • What is the purpose of reading the byte-to-byte string? A filter that removes duplicate whitespace ?

  • @Lacobus, the intention is to remove excesses of spaces, leaving only one space between each word, beginning and end of the line.

  • @Lacobus I must create a new question for that purpose?

Show 1 more comment

3 answers

2


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;
}

1

Another solution(if I understood the problem well) would be you use regular Expressions:

#include <string>
#include <iostream>
#include <regex>
using namespace std;


int main()
{
    regex reg("[ ]+");
    string linha;

    while (getline(cin, linha))
        cout << regex_replace(linha, reg, " ") << "\n\n";

    return 0;
}

All multiple spaces will be replaced by just one.

1

int main(void) {
    FILE *ficheiro = fopen("arquivo.txt", "r");
    if(ficheiro == NULL){
        printf("\nErro");
        exit(1);
    }
    char linha[100];
    while(fgets(linha, 100, ficheiro){ //essa função vai ler todo o ficheiro
        printf("\n%s", linha);
    }
}
  • Hello @clidsaniny, I would like the changes to be made in the code mentioned... if possible.

  • Hello @luccasrodrigo by my vision I find the given example is more effective for your need. If you want an example based on your code, it displays an ex of the.txt file. I look forward to it...

  • Hello @clidsaniny thank you from my heart for the layout, and really the posted examples answer the initial question. However, I believe that in order to solve the major problem, I must draw up a new question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.