-2

I want to parse the following text from a file example_file.txt

# Number of busStop
3
# Id Name Lat Lon
1 Cadorna 450781 76761

using the following example code:

int main() {
    int _numberBus;

    ifstream file;
    file.open("example_file.txt");
    if (file.fail())
      throw runtime_error("Something goes wrong");

    string line;
    getline(file, line); // Skip Comment Line

    getline(file, line);

    istringstream converter; //??
    converter.str(line);     //??
    converter >> _numberBus; //??
}

I am used to reading files in C, but now in C++ this is kind of confusing. I don't understand well the last three lines. Why do I need that so call "converter" object from the istringstream class? My professor said if I recall well that str converts the line to a string. If it was already a string before that, why is it necessary? Why can't I just skip the last three lines and do "_numberBus = (int)line" ?

J.C.VegaO
  • 341
  • 1
  • 6

1 Answers1

1

I'd start answering from the last question.

Why can't I just skip the last three lines and do _numberBus = (int)line

You can't because std::string is a class and line is an object that can't be cast this way to int. std::string is not some typedef of const char*.

Why do I need that so call converter object from the istringstream class?

Because it's a useful abstraction. It's not called stream just randomly. It abstracts a stream of characters. Under the hood, the stream can try to interpret the input sequence of characters as some typed value. In your case, it's an integer number.

user1056837
  • 767
  • 7
  • 27
  • Using stoi(line) worked, instead. Still I don't understing what is the difference with using str? I don't really understand what str does, I read it in the cplusplus site , but its kind of confusing – J.C.VegaO Mar 17 '21 at 11:05
  • What's confusing? `str()` - is just an ordinary getter/setter function – user1056837 Mar 17 '21 at 11:06
  • @ user1056837 The need to use a istringstream object that seems to be just a change of name for a string, and what does str actually does to the object – J.C.VegaO Mar 17 '21 at 11:08
  • 2
    `istringstream` does not "just a change of name for a string". Your C++ textbook will explain that `istringstream` implements formatted extraction operations, line `>>`. That's something that `std::string` will not do. Try using `>>` on the original `std::string` and see how well this works out for you. – Sam Varshavchik Mar 17 '21 at 11:10
  • 2
    @J.C.VegaO `stoi` and `stringstream` are both ways to parse text. `stringstream` can be more straightforward if - for example - you read many inputs from the same string. You can think of a `stringstream` like a simple `string` object wrapped in a more advanced `stream` interface that supports more useful operations (like << and >> operators) – IWonderWhatThisAPIDoes Mar 17 '21 at 11:11
  • @J.C.VegaO is it clear to you what *getter/setter function* means? – user1056837 Mar 17 '21 at 11:12
  • @user1056837 Yes, its just that, who wrote that code called the value converter because it said it was converting from istringstream to string(or the othe wasy around?) So it doesn't look like a setter, because it is changing the type and I am not sure if the input string line is being converted to istringstream or the istringstream object converter is being converted to string( I am refering to converter.str(line)) – J.C.VegaO Mar 17 '21 at 11:16
  • 1
    No, `str` doesn't convert anything to anything. setter function sets some field of an object. In this particular case `stringstream` class has string field inside and `str` function allows to set that private field (or get its value); – user1056837 Mar 17 '21 at 11:21
  • 1
    @J.C.VegaO Nothing is being converted or renamed by `str()`. You create new and independend, `stringstream`, then, you use `converter.str(line)` to load `line` into `converter`'s internal buffer, if you will. – churill Mar 17 '21 at 11:25
  • Oh I see I was misled into thinking it was like a casting. Now it's clear. So the whole point of creating this stringstream object was to be able to use >>, and for that we needed to set the string attribute of the stringstream object equal to that ot the string. And a stringstream object is just a fancy string with enhanced features – J.C.VegaO Mar 17 '21 at 11:31
  • 1
    It's not a fancy string. It's a different abstraction. Streams are not in any sense equal to strings. Streams are for manipulation with stream of some data, e.g. characters. bytes. whatever. Input stream should have a source and in your case this source is a string. – user1056837 Mar 17 '21 at 11:34
  • @user1056837 from http://www.cplusplus.com/reference/iolibrary/ " A stream is an abstraction that represents a device on which input and ouput operations are performed. A stream can basically be represented as a source or destination of characters of indefinite length.". So it says it a bunch of characters that still have to be parsed, but I understand it as row text and text is a string, isnt' it? – J.C.VegaO Mar 17 '21 at 11:43
  • I believe this discussion is out of the scope of this question. You can ask one more question to clarify it. But in short. No, it is not. As I said before. – user1056837 Mar 17 '21 at 12:02