In article <40********@newsgate.hku.hk>,
news.hku.hk <bi******@hkusua.hku.hk> wrote:
is there any way to make a loop to store strings??
e.g. abc.txt contains 3 lines i.e.
this is line 1.
this is line 2.
this is line 3.
i want to create a loop that can make the three strings contains the
corresponding lines,
string line1; //contains "this is line1."
string line2; // contains "this is line2."
string line3; // contains "this is line3."
int no_of_lines = 3;
You can't do this with individually named string variables, but you can
do it with a vector of strings:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
ifstream inputFile ("abc.txt");
vector<string> linevec;
string line;
while (getline (inputFile, line))
{
linevec.push_back (line);
}
for (int k = 0; k < linevec.size(); ++k)
{
cout << linevec[k] << endl;
}
return 0;
}
--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA