User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2007
Posts: 6
Reputation: phylon is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
phylon phylon is offline Offline
Newbie Poster

reading data from file to vector

  #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

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;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: reading data from file to vector

  #2  
Oct 21st, 2007
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;
}
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:48 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC