How to search in txt file?

Asked

Viewed 5,100 times

1

How do I search a file .txt which contains for example a list of numbered names, type:

1 - João
2 - Maria
...

I would inform the desired number and the return would be the name, or say that nothing was found. It could be in C, or C++.

  • If the idea is to do multiple searches it would be advisable to treat the file once at the beginning of the program and save the data in memory until the program is finished.

  • Are the numbers sorted? Match the line number?

  • Yes, I want you to look for the number the user enters

2 answers

1

To find element number 11 uses awk!:

awk '$1==11{print $3}' ficheiro.txt

if you really have to use C

#include <stdio.h>

int main(){
  char s[100]; int n;
  scanf("%d",&n);
  sprintf(s,"awk '$1==%d {print $3}' ficheiro.txt", n);
  system(s);
}
  • 1

    It’s kind of dangerous to use sprintf to mount scripts like you’re doing. In this specific case where the user input is a number has no problem but in general it is easy to forget to slip something and end up with a code injection vulnerability.

  • @hugomg, I agree, (in this case the issue is not problematic but I agree)

1


In C++ you can do as follows:

string getNome(string numero) {

    ifstream ifs( "ficheiro.txt" );
    string linha;
    string resultado = "Nada foi encontrado";

    while( getline( ifs, linha) ) {
        if (linha.find(numero, 0) != string::npos) {
           //cout << "encontrou: " << numero << " linha: " << linha << endl;
           resultado = linha.substr(linha.find('-', 0) + 2, linha.length() );
        }
    }

    return resultado;
}

Following @pmg’s comment, and if the intention is to do multiple searches you can try something like:

#include<iostream>
#include<string>
#include<fstream>
#include<map>

class MapaNomes{
public:
    MapaNomes(string nomeFicheiro) {
       ifstream ifs(nomeFicheiro);
       string linha;
       string numero;

       while(getline(ifs, linha)) {
          numero = linha.substr(0, linha.find('-', 0), -1);
          nome = linha.substr(linha.find('-', 0) + 2, linha.length());

          nomes.insert(pair<int, string>(stoi(numero), name));
      }
    }

    string getNome(int numero) {
        map<int, string>::iterator it = nomes.find(numero);
        return (it != nomes.end()) ? it->second : "Nada foi encontrado";
    }
private:
    map<int, string> nomes;
}

int main(int argc, char* argv[]) {
    MapaNomes mNomes("lista_nomes.txt");

    cout << "Procurar 1, Resultado: " << mNomes.getNome(1) << endl;
    cout << "Procurar 2, Resultado: " << mNomes.getNome(2) << endl;
    cout << "Procurar 3, Resultado: " << mNomes.getNome(3) << endl;
    cout << "Procurar 4, Resultado: " << mNomes.getNome(4) << endl;

    return 0;
}

Assume your file is formatted consistently. If that is not the case, you need to adjust and improve the way the number and the corresponding name are being captured.

But can I ask why I do this in C or C++? It’s just an exercise?

  • Yes it is an exercise to apply certain search methods and check the performance of each. I will test your code thank you

Browser other questions tagged

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