Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read strings into an array

Status
Not open for further replies.

gafman99

Programmer
Jul 26, 2004
1
0
0
GB
Hi, would like some help.

I've got a list in a .txt file, and I'm trying to read each string into an array, so that I can later analyse each string seperately, but I don't know where to start.
Also each txt file list size will vary.

An example of one of the txt files:
T930230
T839384
G309443

Thanks in advance, gafman
 
Try this sceleton:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

vector<string> text;
ifstream f("textfile.txt");
string line;

while (getline(f,line))
  text.push_back(line);
f.close();

cout << text.size() << " lines:" << endl;
for (int i = 0; i < text.size(); ++i)
  cout << text[i] << endl;
No error handling, no any optimization in the snippet.
But it works...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top