•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,564 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,567 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2802 | Replies: 1
![]() |
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
using namespace std;
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <string>
int main(void) {
vector<string>o;
vector<float> a;
FILE *fw;
fw = fopen("input1.txt", "r");
char str[100];
float f;
while (!feof(fw)) {
string newstr="";
while (1) {
int c=fgetc(fw);
// if (c==',' || c=='|'||feof(stdin)) break;
newstr.push_back((char)c);
if(c=='{')
{
while(c!='}'){
int c=fgetc(fw);
// while (fgets(str,sizeof(str),fw)!=NULL){
// fscanf (fw, " %f", &f);
// printf("%f\n",f);
newstr.push_back((char)c);
}
}
}
}
o.push_back(newstr);
a.push_back(newstr);
// }
for (unsigned int i=0;i<o.size(); i++)
printf(" String %d is %s\n", i, o[i].c_str());
for (unsigned int j=0;j<a.size(); j++)
printf(" String %f is %s\n", j, a[j].c_str());
return 0;
}•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
Here is one way to do it -- first read the entire line into a std::string object then parse it
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> sList;
vector<float> fList;
string name;
float f;
string line = "X = {1.0, 2.0, 3.0}";
// extract the name which
size_t pos = line.find(' ');
name = line.substr(0,pos);
line = line.substr(pos+1);
pos = line.find('{');
line = line.substr(pos+1);
while( (pos = line.find(',')) != string::npos)
{
string n = line.substr(0,pos);
f = (float)atof(n.c_str());
fList.push_back(f);
line = line.substr(pos+1);
}
// now do the last float in the line
f = (float)atof(line.c_str());
fList.push_back(f);
return 0;
}![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- problems with reading in file (C++)
- A function which will transfer the data from a file to STL( vector or map) object. (C++)
- Quick TT_EOL question (Java)
- Reading binary data from a file and writing it (Visual Basic 4 / 5 / 6)
- Reading an input file as a class memeber function (C++)
- Perl/CGI (Reading Data) Part II (Computer Science and Software Design)
- reading txt file into array (C++)
- Reading from external data file (C++)
Other Threads in the C++ Forum
- Previous Thread: Data Table
- Next Thread: File read problem



Linear Mode