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.

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

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