0

I have following code to read a text file line by line and store data in a string* variable:

#include <string>
#include <fstream>
#include <iostream>
using namespace std;

int main(){
    // open a file in read mode.
   ifstream infile; 
   infile.open("iris.csv"); 

    // read file line by line and store here: 
    string *strlist; 
    int count =0; 
    for (string line; getline(infile, line); ) {
        strlist[count] = line; 
        count++; 
    }
    infile.close(); 
    //print all lines: 
    for (int i=0; i<count; i++){
        cout << strlist[i];
    }
}

Will above work or will there be some memory problem. Do I need to make a new string before assigning a value strlist[i]=line?

I did not try it fearing that an error may cause data damage. Thanks for your help.

rnso
  • 23,686
  • 25
  • 112
  • 234
  • strlist is uninitialized pointer. Use vector instead. – Eugene Sep 20 '19 at 02:53
  • Yes, vector is very useful. Thanks. `Vector.push_back` is `first in, first out` or `first in, last out`? – rnso Sep 20 '19 at 03:05
  • You can use push_back() and pop_back() to push and pop element respectively from vector (always from back). From that point of view, it's first in last out. However, you can also delete element at a specific index (or range) using erase. – Kaushik Roy Sep 20 '19 at 04:34

2 Answers2

0

Using vector is a good option.

#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;

int main(){
    // open a file in read mode.
   ifstream infile; 
   infile.open("iris.csv"); 

    // read file line by line and store here: 
    vector<string> strlist; 

    for (string line; getline(infile, line); ) {
    //or
    //while (getline(infile, line)) {
        strlist.push_back(line); 
    }
    infile.close(); 
    //print all lines: 
    for (int i=0; i<strlist.size(); i++){
        cout << strlist[i];
    }
}
Kaushik Roy
  • 1,627
  • 2
  • 11
  • 13
  • Yes, vector is very useful. Thanks. – rnso Sep 20 '19 at 03:05
  • How is `for (string line; getline(infile, line); )` working? There is no third component here. – rnso Sep 20 '19 at 03:06
  • @rnso this just popped up because of another question: here's a slightly shorter way to do this: https://stackoverflow.com/questions/16727125/how-does-stdcopy-work-with-stream-iterators – user4581301 Sep 20 '19 at 03:09
  • @rnso The value returned by getline is a reference to the stream object itself, which when evaluated as a boolean expression is true if the stream is ready for more operations, and false if either the end of the file has been reached or if some other error occurred. So, for/while loop breaks when false is returned by getline. In case of for loop, it's ok to keep 3rd statement empty as we don't need to execute any statement after each loop. – Kaushik Roy Sep 20 '19 at 04:20
-3

I think if you want to load the file all in one time in c++, it must will crush your memory. just read one line and do something on it or make a counter to remember how much you had loaded.After you finish one part then go for next one.

whujjq
  • 1