944,008 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 10774
  • C++ RSS
Oct 21st, 2007
0

reading data from file to vector

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
phylon is offline Offline
6 posts
since Oct 2007
Oct 21st, 2007
0

Re: reading data from file to vector

Here is one way to do it -- first read the entire line into a std::string object then parse it
C++ Syntax (Toggle Plain Text)
  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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Data Table
Next Thread in C++ Forum Timeline: File read problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC