reading data from file to vector

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 6
Reputation: phylon is an unknown quantity at this point 
Solved Threads: 0
phylon phylon is offline Offline
Newbie Poster

reading data from file to vector

 
0
  #1
Oct 21st, 2007
I am trying to read matrix data from file into a float vector

the file has data like

X = {1.0, 2.0, 3.0...........}
Y = {3.4, 3.1, 3.4...........}

I can read into vector strings. But I want to store the matrix names in a string vector and the matrix in a float vector

here is the code

  1. using namespace std;
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <vector>
  5. #include <map>
  6. #include <string>
  7.  
  8. int main(void) {
  9. vector<string>o;
  10. vector<float> a;
  11.  
  12. FILE *fw;
  13.  
  14. fw = fopen("input1.txt", "r");
  15.  
  16. char str[100];
  17. float f;
  18. while (!feof(fw)) {
  19. string newstr="";
  20. while (1) {
  21. int c=fgetc(fw);
  22. // if (c==',' || c=='|'||feof(stdin)) break;
  23. newstr.push_back((char)c);
  24. if(c=='{')
  25. {
  26. while(c!='}'){
  27. int c=fgetc(fw);
  28. // while (fgets(str,sizeof(str),fw)!=NULL){
  29. // fscanf (fw, " %f", &f);
  30. // printf("%f\n",f);
  31. newstr.push_back((char)c);
  32. }
  33.  
  34. }
  35. }
  36. }
  37. o.push_back(newstr);
  38. a.push_back(newstr);
  39. // }
  40.  
  41. for (unsigned int i=0;i<o.size(); i++)
  42. printf(" String %d is %s\n", i, o[i].c_str());
  43.  
  44. for (unsigned int j=0;j<a.size(); j++)
  45. printf(" String %f is %s\n", j, a[j].c_str());
  46.  
  47. return 0;
  48. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: reading data from file to vector

 
0
  #2
Oct 21st, 2007
Here is one way to do it -- first read the entire line into a std::string object then parse it
  1. int _tmain(int argc, _TCHAR* argv[])
  2. {
  3. vector<string> sList;
  4. vector<float> fList;
  5. string name;
  6. float f;
  7. string line = "X = {1.0, 2.0, 3.0}";
  8.  
  9. // extract the name which
  10. size_t pos = line.find(' ');
  11. name = line.substr(0,pos);
  12. line = line.substr(pos+1);
  13. pos = line.find('{');
  14. line = line.substr(pos+1);
  15. while( (pos = line.find(',')) != string::npos)
  16. {
  17. string n = line.substr(0,pos);
  18. f = (float)atof(n.c_str());
  19. fList.push_back(f);
  20. line = line.substr(pos+1);
  21. }
  22. // now do the last float in the line
  23. f = (float)atof(line.c_str());
  24. fList.push_back(f);
  25. return 0;
  26. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC