I am trying to learn C++ ifstream i came across this code on the net and have some question.

void read() {
    ifstream  inFile ("in.txt");
        string  l, i;
        int item = 0;

        while (getline (inFile, l)) { // is this one checking end of file like java?

        istringstream linestream(l); // what does this one do?
        while (getline (linestream, i, ' ')) {

            item++;
            if (item <= 3) {
                initial.push_back(atoi(i.c_str()));
                                                  //atoi(i.c_str()) what is this?
                                                  // initial is a vector like ArrayList in java
            }
            else {
                final.push_back(atoi(i.c_str()));
            }
        }
    }

Thanks

Re: C++ question about ifstream 80 80

Hi Banh, welcome to DaniWeb!

The istringstream is a stringstream - it lets you "manipulate strings as if they were input/output streams."
http://www.cplusplus.com/reference/iostream/stringstream/

You don't show how you have defined 'initial', but atoi could probably be replaced by another stringstream operation.

Also, please use code tags when posting code.

David

Re: C++ question about ifstream 80 80

I am trying to reference it to Java so I can understand it better.
the initial is

vector<int> initial;

it works very similar to Java ArrayList, but other than that I need help explaining the rest.

Re: C++ question about ifstream 80 80

Unfortunately I don't know Java, but you can get a good idea from here:
http://www.cplusplus.com/reference/stl/vector/

If you initialize it like you have done, it will have 0 elements. Using push_back(element) will extend the vector and insert the element in the last position. Once the vector has some contents, you can access elements with initial.

Several other things you can do with vectors are shown here:
http://programmingexamples.net/index.php?title=CPP/STL/Vector

David

Re: C++ question about ifstream 80 80

Thanks
quick question

void read() {
      ifstream inFile ("in.txt");
      string l, i;
      int item = 0;
 
      while (getline (inFile, l)) { 
         istringstream linestream(l);
         while (getline (linestream, i, ' ')) {
             item++;
             if (item <= 3) {
                  initial.push_back(atoi(i.c_str()));
                                   //atoi(i.c_str()) what is this?
                                   // initial is a vector like ArrayList in java
         }
         else {
         final.push_back(atoi(i.c_str()));
     }
  }
}

say if I have the input file:
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18

the final result in initial = {1,2,3} type Integer
and final = {4,5,6} type Integer
if both final and initial are vector<int>

Re: C++ question about ifstream 80 80

I don't follow you, can you clarify? What are you trying to do?

Re: C++ question about ifstream 80 80

Just trying to understand what the code does if have the "in.txt" as above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.