1

I'm trying to get my hands dirty with File IO here. I'm able to do basic stuff but kinda ran out of ideas here. I want to read input from a text file. Each line of the text file follows this format: <int> <char> <string literal> <float>, if it doesn't it is to be ignored.

And for each line, we need to store the variables in say four data types:

int i;
char c;
std::string  s;
float f;

And print them. For the next line, they are overwritten and printed again.

Here's my attempt:

int main() {
    int i;
    char c;
    std::string  s, x;
    float f;
    ifstream in;
    in.open("Input.txt");
    if(in) {
        std::getline(in,x);
        // How do I extract i, c, s and f components from x now?
        cout << "\nInteger:" << i << ", Character: " << c << ", String: " 
             << s << ", Float: " << f << endl; 
    }
    return 0;
}

PS: Despite efficiency bottlenecks, please use only elementary concepts to solve this issue, not advanced stuff.

4
  • What's the question/problem? Splitting the line (string) x into 4 pieces? Commented Apr 7, 2015 at 20:30
  • google sscanf it works like scanf but with strings Commented Apr 7, 2015 at 20:32
  • Yeah, 4 different data types. Commented Apr 7, 2015 at 20:32
  • @JoshuaByer: I'm very reluctant to follow anything but what's shared here on SO because most of the sites contain mistakes. I'm talking about cppreference, etc Commented Apr 7, 2015 at 20:33

1 Answer 1

3

You can simply do:

while(in >> i >> c >> s >> f)
{
    cout << "\nInteger:" << i << ", Character: " << c << ", String: " 
         << s << ", Float: " << f << endl; 
}

If your values are comma-separated, you can use a std::stringstream/std::getline combination to parse the tokens (std::getline allows specifying a separator), like so:

std::stringstream ss; // from <sstream>
int field = 0; 
while (std::getline(in, ss, ','))
{
    switch (field)
    {
    case 0:
        ss >> i; 
        break;
    case 1:
        ss >> c;
        break;
    case 2: 
        ss >> s;
        break;
    case 3:
        ss >> f;
        break;
    }
    if(++field == 4)
        field = 0;
}

Or, you can read the whole line, remove the commas (std::remove_if), send the transformed line into a stringstream, then do ss >> i >> c >> s >> f.

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

11 Comments

I think when the string is reached, this will lead to a problem (unless the string can never contain white space).
@LogicStuff ahh, I though OP used <int> just to denote an int, not that the actual < > were part of the file.
@mah that's true... then it's not as simple, you need to write a parser, or use std::regex
Does this automatically ignore the lines that don't follow this format and smoothly transition to the next line without crashing?
@BloodBrother no, all lines have to have the same format, otherwise the stream end up having a failbit set.
|

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.