Re: newbie question about data I/O
"Seeker" <zhongming@gmail.com> a ?crit dans le message de news:
af4c91a8-a601-47e3-b25a-3587cdd7f6f7@p9g2000vbl.googlegroups.com...
Howdy, gurus
I want to write code to read in a large genomic file. The data look
like
Marker location freq T mu sigma_2 S p-
value
rs2977670 713754 0.925 779 9.604 141.278 2.202 0.02763
rs2977656 719811 0.992 793 9.120 134.796 2.733 0.00627
Here is my code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char** argv)
{
vector<string> snp_list1,snp_list2,snp_list3;
vector<int> location1,location2,location3;
vector<double> freq1,freq2,freq3;
vector<int> T1,T2,T3;
vector<double> mu1,mu2,mu3;
vector<double> sigma_21,sigma_22,sigma_23;
vector<double> S1,S2,S3;
vector<double> p1,p2,p3;
//read in 1st data file;
FILE *in=fopen(argv[1],"r");
char line[128];
fgets(line,128,in); //skip the 1st row;
while (fgets(line,128,in))
{
cout << line << endl;
char *str = strtok(line, "\t"); // the space in "\t" is a tab
string marker(str);
snp_list1.push_back(marker);
str = strtok(NULL, "\t");
location1.push_back(atof(str));
str = strtok(NULL, "\t");
freq1.push_back(atof(str));
str = strtok(NULL, "\t");
T1.push_back(atof(str));
str = strtok(NULL, "\t");
mu1.push_back(atof(str));
str = strtok(NULL, "\t");
sigma_21.push_back(atof(str));
str = strtok(NULL, "\t");
S1.push_back(atof(str));
str = strtok(NULL, "\t");
p1.push_back(atof(str));
}
fclose(in);
//verify the vectors
for (int i=0; i<snp_list1.size();++i)
cout << snp_list1[i] << endl;
return 0;
}
I tried to run the code but always met errors shown as "error while
dumping state..(core dumped)". I am new to C++.Thanks a lot for your
input.
Here is a simple code to read some numbers in a text file using fstream and
stringstream
Here is my text file
SomeString 1 2 3 4 5
now I can read this like that
#include <fstream> // for ifstream
#include <sstring> // for istringstream
....
int main()
{
ifstream ifs("file.txt"); // this open the file in text mode by default
string strLine;
vector<int> v;
getline(ifs, strLine);
istringstream iss(strLine);
iss >> strLine; // extract the first element, we assume it is a string
// now loop until the end of the line and extract every integer
while(!iss.eof())
{
int tmp;
iss >> tmp;
v.push_back(tmp);
}
return (0);
}
It should be ewasy to modify that to read your file. Note that I didn't do
much error checking.
Eric