0

My problem is I am trying to load a Linux formatted text file and read it regularly as is one was opening up a windows formatted text file in a C++ application. I have gotten it to work perfectly when the file is formatted exactly how I want it to be in windows and have the data form the file loaded into a list of lists.

To try to describe my problem a little better what I am currently able to do right now is if I have a file which is tab delimited I am able to store all of the contents from each row into a list of strings where each string is whatever each tab is separating. I then have a list of all of the rows.

For example my text file I'm reading my look something like this:

156   Hit   83.2   23:34
23    Miss  21.4   23:38

and so on....

This code spinet below is what I have been using, which I had found help elsewhere and altered it to work how I needed it to. It will create a list with two items in the list where each of the items contains a list of 4 strings each string representing the contents in each of "columns" for the current row. Hope this is explained thorough enough.

    ifstream infile(file);
list <list <string> > data;
while (infile){
    string s;
    if (!getline( infile, s )) break;

        std::istringstream ss ( s );
        list <string> record;

    while (ss){
        string s;
        if (!getline( ss, s, '\t' )) break;
            record.push_back( s );
    }
    data.push_back( record );
}

That is exactly what I would like to do however instead of the text file I would be reading from being formatted as a Windows text file it will be a Linux text file and will not have a tab in-between each "item" in each row; but instead will contain a random number of spaces. My thought was I could open the file up in binary mode and read it that way and instead of having a tab be my delimiter I could choose any amount of white space. However I am not exactly sure how to do that as I am still relatively new to C++ and have not specifically worked with reading Linux formatted text files from a Windows C++ application. Any help would be greatly appreciated. Thanks in advance!

1 Answer 1

2

This has nothing to do with Linux versus Windows. You can use >> to perform formatted input of whitespace-separated fields:

string s;
while (ss >> s)
    record.push_back(s);

To skip whitespace explicitly, use std::ws; to disable whitespace skipping, use std::noskipws.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.