By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,275 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,275 IT Pros & Developers. It's quick & easy.

Read data from text file into vector

P: 8
Hello,

i have a problem where i need read dataset from text file and keep them into a vector.

e.g. dataset:

"0012 0033 00045 0044 0047"

Based on this dataset, i need to keep every 4 numbers separately into a vector.

: P
Feb 14 '07 #1
Share this Question
Share on Google+
6 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Hello,

i have a problem where i need read dataset from text file and keep them into a vector.

e.g. dataset:

"0012 0033 00045 0044 0047"

Based on this dataset, i need to keep every 4 numbers separately into a vector.

: P
Did you have a question about that? In declaring/manipulating a vector or reading input in from a text file?
Feb 14 '07 #2

P: 8
arf
Did you have a question about that? In declaring/manipulating a vector or reading input in from a text file?
The basic idea is to transfer the dataset from a text file into a vector. From this vector, the manipulation process will be conducted. This dataset as been inserted as "string" in the vector.e.g. "0012 0034 0045 0023 3343 0001". I do not know how to convert/divide this string into a group of characters .."0012" , " 0034" and etc so that i could keep them as a separated data in the vector. Based on my code, each vector will keep the data for the whole string. What is wrong with my code?

This is my code for your reference

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.         SDTVECTOR Std;
  4.         SDT TpStd;                  //declared TpStd -- vector<string>
  5.         string x;
  6.  
  7.         ifstream infile;
  8.         infile.clear();
  9.         infile.open("staf.txt");
  10.  
  11.        if (!infile) 
  12.       {
  13.           cerr << "File could not be opened" << endl;
  14.           exit(1);
  15.       }   
  16.  
  17.       while (getline(infile, x, '\n'))
  18.  
  19.      {
  20.     TpStd.Em.push_back(x);
  21.     {
  22.         Std.push_back(TpStd);
  23.         TpStd.Em.erase(TpStd.Em.begin(), TpStd.Em.end());
  24.     }
  25.      }    
  26. .
  27.  
  28. .
  29. .
  30. }return;
Feb 14 '07 #3

Expert 100+
P: 1,510
the statement
Expand|Select|Wrap|Line Numbers
  1. while (getline(infile, x, '\n'))
  2.  
read whole lines at a time, replace it with
Expand|Select|Wrap|Line Numbers
  1. while (infile >> x)
which will read words delimited by whitespace
Feb 14 '07 #4

P: 8
arf
Thanks..It's work. But how about, if there is a several strings and each string is assumed as a single respondent..e.g.

"0111 0233 9449 0022"
"4453 8899 3343 0003"
"4456 4453 2223 1223"

* this case, there are 3 respondents

The question is how the program will know once it reaches at the end of string.
Feb 14 '07 #5

Expert 100+
P: 1,510
Thanks..It's work. But how about, if there is a several strings and each string is assumed as a single respondent..e.g.

"0111 0233 9449 0022"
"4453 8899 3343 0003"
"4456 4453 2223 1223"

* this case, there are 3 respondents

The question is how the program will know once it reaches at the end of string.
Expand|Select|Wrap|Line Numbers
  1. while (infile >> x)
will read whitespace delimeted strings until end of file or error
Feb 14 '07 #6

P: 8
arf
Expand|Select|Wrap|Line Numbers
  1. while (infile >> x)
will read whitespace delimeted strings until end of file or error

Ok.

If i would like to maintain these line of strings (because each string represent for each respondent) put all of data into the vector, where from there i could count or compare them based on these respondents. Should i need to use '\n' in order to separate these strings?

*I need to use the vector because the data is fluctuate from one string to another
Feb 14 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.