Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading in file with delimiter

How do I read in lines from a file and assign specific segments of that line to the information in structs? And how can I stop at a blank line, then continue again until end of file is reached?

Background: I am building a program that will take an input file, read in information, and use double hashing for that information to be put in the correct index of the hashtable.

Suppose I have the struct:

struct Data
{
    string city;
    string state;
    string zipCode;
};

But the lines in the file are in the following format:

20

85086,Phoenix,Arizona
56065,Minneapolis,Minnesota

85281
56065

Sorry but I still cannot seem to figure this out. I am having a really hard time reading in the file. The first line is basically the size of the hash table to be constructed. The next blank line should be ignored. Then the next two lines are information that should go into the struct and be hashed into the hash table. Then another blank line should be ignored. And finally, the last two lines are input that need to be matched to see if they exist in the hash table or not. So in this case, 85281 is not found. While 56065 is found.

like image 621
user3399575 Avatar asked Mar 10 '14 01:03

user3399575


3 Answers

As the other two answers point out you have to use std::getline, but this is how I would do it:

if (std::getline(is, zipcode, ',') &&
    std::getline(is, city,   ',') &&
    std::getline(is, state))
{
    d.zipCode = std::stoi(zipcode);
}

The only real change I made is that I encased the extractions within an if statement so you can check if these reads succeeded. Moreover, in order for this to be done easily (you wouldn't want to type the above out for every Data object), you can put this inside a function.

You can overload the >> operator for the Data class like so:

std::istream& operator>>(std::istream& is, Data& d)
{
    std::string zipcode;
    if (std::getline(is,  zipcode, ',') &&
        std::getline(is, d.city,   ',') &&
        std::getline(is, d.state))
    {
        d.zipCode = std::stoi(zipcode);
    }

    return is;
}

Now it becomes as simple as doing:

Data d;

if (std::cin >> d)
{
    std::cout << "Yes! It worked!";
}
like image 162
David G Avatar answered Oct 19 '22 22:10

David G


You can use a getline function from <string> like this:

string str;                      // This will store your tokens
ifstream file("data.txt");

while (getline(file, str, ',')   // You can have a different delimiter
{
     // Process your data

}

You can also use stringstream:

stringstream ss(line);           // Line is from your input data file
while (ss >> str)                // str is to store your token
{
     // Process your data here
}

It's just a hint. Hope it helps you.

like image 25
Khanh Nguyen Avatar answered Oct 19 '22 21:10

Khanh Nguyen


All you need is function std::getline

For example

std::string s;
std::getline( YourFileStream, s, ',' );

To convert a string to int you can use function std::stoi

Or you can read a whole line and then use std::istringstream to extract each data with the same function std::getline. For example

Data d = {};

std::string line;

std::getline( YourFileStream, line );

std::istringstream is( line );

std::string zipCode;

std::getline( is, zipCode, ',' );

d.zipCode = std::stoi( zipCode );

std::getline( is, d.city, ',' );
std::getline( is, d.state, ',' );
like image 1
Vlad from Moscow Avatar answered Oct 19 '22 21:10

Vlad from Moscow