2

I'm trying to figure out what is the best way of handling text input like if I were to use fscnaf in C.

The below seems to work for a text file that contains...

string 1 2 3
string2 3 5 6

As I want it too. It reads the individual elements on each line and puts them into their respective vectors. Would you say this is a good way of handling the input? The input will always start with a string and then followed by the same count of numbers on each line.

int main(int argc, char* argv[])
{
ifstream inputFile(argv[1]);

vector<string> testStrings;
vector<int> intTest;
vector<int> intTest2;
vector<int> intTest3;
string testme;
int test1;
int test2;
int test3;

if (inputFile.is_open())
{
    while (!inputFile.eof())
    {
        inputFile >> testme;
        inputFile >> test1;
        inputFile >> test2;
        inputFile >> test3;

        testStrings.push_back(testme);
        intTest.push_back(test1);
        intTest2.push_back(test2);
        intTest3.push_back(test3);
    }
    inputFile.close();
}
else
{
    cout << "Failed to open file";
    exit(EXIT_FAILURE);
}
return 0;
}

UPDATE

I have changed the while loop to this...is it any better?

    while (getline(inputFile, line))
    {
        istringstream iss(line);

        iss >> testme;
        iss >> test1;
        iss >> test2;
        iss >> test3;

        testStrings.push_back(testme);
        intTest.push_back(test1);
        intTest2.push_back(test2);
        intTest3.push_back(test3);
    }
gsamaras
  • 71,951
  • 46
  • 188
  • 305
cpd1
  • 777
  • 11
  • 31
  • If you are attempting to process input from a text file, that contains individual lines of text, with each line of text terminated by a newline character, the appropriate way to parse this input is to use std::getline() to read each individual line. That's what std::getline() is for. Only then, after the entire line has been read, you should be attempting to parse its contents in some way. Constructing a `std::istringstream` from the read line would be one reasonable approach, but that's not the only one. – Sam Varshavchik Apr 02 '16 at 14:46
  • I tried that but got confused on how to handle the getline. The way I've handled it before is doing char by char checking after I used getline. I'm trying to move away from that. – cpd1 Apr 02 '16 at 14:47
  • `while (!inputFile.eof())` will fail you. Read more here: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – user4581301 Apr 02 '16 at 14:49
  • Are you saying to use getline as well? It works so far. Not saying I'm doing it the correct way - that's why I'm here. – cpd1 Apr 02 '16 at 14:50
  • I made a change to my loop - would you guys mind checking? – cpd1 Apr 02 '16 at 15:03

1 Answers1

2

For your code, read this please: Why is iostream::eof inside a loop condition considered wrong?


Since you know the format, use ifstream, you could easily write less code to achieve same (or a bit better result):

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

int main(int argc, char* argv[]) {
        std::ifstream ifs;
        if(argc > 1) {
                ifs.open(argv[1]);
        } else {
                std::cout << "Usage: " << argv[0] << " <filename>\n";
                return -1;
        }
        std::string str;
        int v1 = -1, v2 = -1, v3 = -1
        if (ifs.is_open()) {
                while(ifs >> str >> v1 >> v2 >> v3)
                        std::cout << str << ' ' << v1 << ' ' << v2 << ' ' << v3 << std::endl;
        } else {
                std::cout << "Error opening file\n";
        }
        return 0;
}

Output:

gsamaras@gsamaras:~$ g++ -Wall readFile.cpp 
gsamaras@gsamaras:~$ ./a.out test.txt 
string 1 2 3
string2 3 5 6

I was inspired by this: How to read formatted data in C++?

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • So I essentially can skip the getline and just have while loop check for what I'm getting since it will fail if the expected input is incorrect? Didn't think of that but makes sense! Also didn't think of just putting the ifstream on one line. Thanks! – cpd1 Apr 02 '16 at 15:23
  • I didn't now that too before making a similar question like yours here in SO, somebody answered back then, letting me know, now I am the one to let you know! Make sure you let someone else know in the future @cpd1! :D – gsamaras Apr 02 '16 at 15:25
  • 1
    Ah nice...paying it forward :) Will try to do the same...thank you! – cpd1 Apr 02 '16 at 15:30
  • @cpd1 this reminded me "Pay It Forward", the movie, I will upvote an answer I like from yours, since this was a very touching movie! – gsamaras Apr 02 '16 at 15:39