简体   繁体   中英

C++ Read Tab Delimited File Into Vector and Display It

Im trying to read a tab delimited file into a string vector and display it. But I am not getting the desired output.

This is the file I am trying to read in and display:

1   amazon  billybob@kfc.com    password!23
2   facebook    digitalGlut3n@ello.mail fri3dMayoNaize
3   bank.com    brokeDude@sofa.com  LostTheRemot3!.Crap

This is my display function:

void DisplayRecords() {

    vector<string> vRecords;
    ifstream inFile("database.txt");

    string entry;

    while (inFile >> entry) {
        vRecords.push_back(entry);
    }

    for (int i = 0; i < vRecords.size(); i++) {
        if (i % 4 == 0) {
            cout << "\n";
        }
        cout << setw(5) << vRecords[i];
    }

    cout << "\n\n";
}

And this is the output I get:

1amazonbillybob@kfc.compassword!23
2facebookdigitalGlut3n@ello.mailfri3dMayoNaize
3bank.combrokeDude@sofa.comLostTheRemot3!.CrapPress any key to continue . . .

How would I get my function to display similar to the original file with spaces between strings?

A vector of individual strings really does not make a lot of sense for structured data. A vector of structs would make more sense, eg:

#include <iostream>
#include <vector>
#include <string>
#include <sstream>

struct Record
{
    std::string id;
    std::string site;
    std::string user;
    std::string pass;
};

void DisplayRecords()
{
    std::vector<Record> vRecords;
    std::ifstream inFile("database.txt");
    std::string entry;

    while (std::getline(inFile, entry))
    {
        std::istringstream iss(entry);

        Record rec;
        std::getline(iss, rec.id, '\t');
        std::getline(iss, rec.site, '\t');
        std::getline(iss, rec.user, '\t');
        std::getline(iss, rec.pass, '\t');

        vRecords.push_back(rec);
    }

    for (int i = 0; i < vRecords.size(); ++i)
    {
        Record &rec = vRecords[i];
        std::cout << std::setw(5)  << rec.id
                  << std::setw(10) << rec.site
                  << std::setw(30) << rec.user
                  << std::setw(30) << rec.pass
                  << "\n";
    }

    std::cout << "\n";
}

if your database file is fixed format.

you can get answer through below code.

void DisplayRecords() {

  vector<string> vRecords;
  ifstream inFile("database.txt");

  string entry;

  while (inFile >> entry) {
    vRecords.push_back(entry);
    vRecords.emplace_back("\t");
  }

  for (int i = 0; i < vRecords.size(); i++) {
    if (i % 8 == 0) {
      cout << "\n";
    }
    cout << setw(5) << vRecords[i];
  }

  cout << "\n\n";
}

in this code, entry variable is 1, amazon, .... in while loop.

so, i added tab between each strings.

and print new line, when i%8 == 0 is true.

i wish it was helpful to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM