Friday, July 2, 2010

read in file to vector arrary --C++

#include <ctime>
#include<iomanip>

#include <iostream>
#include<string>
using namespace std;




void read_network(vector <vector<int> > &VCMA_network){

//read network from input files

char *file;
ifstream fin;
string path,name;


path="/global/scratch/jiansen6/network-input/";
name=path+"stubs.dat1";
cout <<"input file name "<<name<<endl;

file=new char[name.size()+1];
strcpy (file, name.c_str());

fin.open(file);


if (fin.is_open()){

string line;
while ( getline(fin, line) ) {

istringstream ss(line);
vector < int > data;
int value;

// for data separate by space 
                      while (ss >> value)
{
data.push_back(value);
}

// for data separate by comma
/*
char output[30];
while ( ss.getline(output, sizeof(output),',') ){
value = atoi(output); //convert char to int
data.push_back(value);}

*/
VCMA_network.push_back(data);

}//end of while
        } //end of fin.is_open
        else{

cout<<"Problem with opening the file "<<name<<endl;
exit(1);
}

fin.close();

}

No comments:

Post a Comment