0

Hey guys, i am having a problem in getting to store data in a text file.

I have a certain text file which contains data in this format...

Jimeno
Rames
James
Kimon

what i am tryng to do is to store each line into a seperate value. The problem i face is that the number of lines in my text file might defer, in some cases there can be 20 names inside a file.

What i am trying to achieve is for some way i can store each line into a variable to be used later on.

the coding i have come up with to read the text file is ....

#include <stdlib.h>
#include <iostream>
#include <fstream>

using namespace std ;
int main ()
{


fstream readFile("Note.txt");
string templine ;
string line ;

while (getline(readFile,templine))
{
 
            cout << "templine" << endl;
     
}

}

from here on, i am quite lost. i was thinking of using arrays but how do i do it if i am not able to know the number of arrays that should be created and how do i store each value into a seperate variable? hope for some help here.

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by jonsca
0

Since you're using C++, then std::vector< std::string > lines; would save you from knowing in advance how many lines there are.
Look up the push_back method for vector.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.