0

My input file userinfo.csv contains username and password in this format username,password shown below.

frierodablerbyo,Rey4gLmhM
pinkyandluluxo,7$J@XKu[
lifeincolorft,cmps9ufe
spirginti8z,95tcvbku

I want to store all the usernames and passwords in

vector<string> usernames;
vector<string> passwords;

I've never used C++ for file handling, only python

EDIT1

#include <bits/stdc++.h>
using namespace std;

int main()
{
    fstream myfile;
    myfile.open("small.csv");

    vector<string> data;
    vector<string> usernames, passwords;

    while(myfile.good()){

        string word;
        getline(myfile, word, ',');
        data.push_back(word);
    }
    for(int i=0; i<8; i=i+2){
        usernames.push_back(data[i]);
    }
    for(int i=1; i<8; i=i+2){
        passwords.push_back(data[i]);
    }
}

I know above code is bad, how can I improve it because my actual csv file contains 20000 rows.

Kal-EL
  • 3
  • 2
  • Open the file (look for [`std::ifstream`](https://en.cppreference.com/w/cpp/io/basic_ifstream)), read it line by line ([`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline)) and then split the strings at the comma. – Aconcagua Nov 23 '21 at 08:34
  • Hello, people on this site likes to see that OP put some effort before asking the question. Can you please show what have you tried so far? There are many examples of this online. – no more sigsegv Nov 23 '21 at 08:35
  • 1
    Recommendation: Do not split data that belongs together, better store them e. g. in a `std::map`, a vector of pairs or a vector of a custom struct with name and password members. Two separate vectors is pretty error prone, as you have to do any modifications *always* in both vectors in parallel (-> duplicate code at some point). – Aconcagua Nov 23 '21 at 08:36
  • You should keep an eye on [including bits/stdc++.h](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)! – Aconcagua Nov 23 '21 at 09:03
  • Refer to [this](https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c) for reading csv – Adam Nov 23 '21 at 09:44

2 Answers2

2

The code snipplet already posted is fine, but keep in mind that the CSV separators are locale-dependent, e. g. for US its a ',', for Germany it would be ';' and so on. Also if you have text sections in your CSV which might contain one of those characters, you have to check for opening and closing quotation marks.

The most easy thing to do is to use a ready-made library for parsing CSVs, for example https://github.com/d99kris/rapidcsv.

ouflak
  • 2,458
  • 10
  • 44
  • 49
borealis-c
  • 146
  • 1
  • 6
0

You can try something like this

std::vector <std::pair<std::string, std::string>> vec_credentials;

std::ifstream is("credentials.csv");
if(is.is_open())
{
    std::string line;
    while(getline(is, line))
    {
        std::stringstream ss(line);
        std::string token;
        std::vector <std::string> temp;
        // this is good if in the future you will have more than 2 columns
        while(getline(ss, token, ','))
        {
            temp.push_back(token);
        }
        vec_credentials.push_back(std::make_pair(temp[0], temp[1]));
    }
    is.close();
}
no more sigsegv
  • 468
  • 5
  • 17