Ifstream?

Time to post in the Beginners section. So embarassing.

Now, now.
Until now i've been doing file handling with C streams and separating whitespace with my bare hands.
I'd like a C++11 equivalent that doesn't make me put my hands in a place to write '\r', '\n', or "\r\n" anymore.

The exact question is:
Given the following input...
 key = value 
k e y = value
k ey= v alue
 ke y =va lue


How in the world do I skip the (optional) whitespaces, and retrieve both key and value?
All the four keys listed above are DIFFERENT from each other. Spaces in the key must be kept.
Both key and value may contain whitespaces. Only value may contain a "=" character.

May this seem like a school-related question, it is NOT.
I'm not at school.
After switching to C++11, I was just looking to make a completely portable program, which happens to deal with some configuration thingies.

I'm not looking for a straight answer.
Just give me tips like:
How to skip all whitespaces - How to read all whitespaces - How to skip a specific character
Last edited on
So I guess no std::-ish things?
Guess I'll go old-style.

EDIT: Whoops, that post must have been deleted.
Last edited on
To skip or not skip whitespaces: http://www.cplusplus.com/reference/ios/skipws/

To skip out any character (including a space).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

int main()
{
    int x = 0;
    std::string toread = "";
    std::cout << "Enter a line of text. Lower case letter f will be ignored.\n";
    getline(std::cin, toread);
    std::cout << "Original: " << toread << "\n";
    while(x < toread.length())
    {
        if(toread.at(x) == 'f') //Search the string for f
        {
            toread.replace(x,1, ""); //Replace with nothing
        }
        ++x;
    }
    std::cout << "Modified: " << toread << "\n"; //Output the modified string.
    return 0;
}
Last edited on
I like the std::string::replace idea.
Will edit the loop for my needs, and will make sure to engroup whitespace deletion groups together.
Topic archived. No new replies allowed.