Hello Everyone! I'm a beginner in c++ and i'm trying to import a csv file and use the data it has to perform some operations. The file in question is fairly large and has 2800+ rows and 3 columns. I want to ignore the first row and column since they're only labels and store the numbers(as integers) in the last two columns in arrays. The only code i have typed so far is really only:
@chicofeo after some sleep and some googling i found out a way to do it but when i run the code it doesn't read in the last column for some reason. It's just a bunch of zeros.
int main()
{
int Geneinfo[2823][3];
string item;
ifstream myfile;
myfile.open("/Users/T_HEN1203/Desktop/Dark Aerobic.csv");
if (myfile.is_open())
{
// for loop for converting the strings into ints and storing them in a multidimensional array
for (int row(0); row < 2823; ++row)
{
getline(myfile, item);
stringstream iss(item);
for (int col(0); col < 3; ++col)
{
string line;
getline(iss, line, ',');
// used to break loop if files stops being read
if ( !iss.good() )
{
break;
}
stringstream convertor(line);
convertor >> Geneinfo[row][col];
}
}
// used to display the array
for (int r = 0; r < 2823; ++r)
{
for (int c = 0; c < 3; ++c)
{
cout << Geneinfo[r][c] <<" ";
}
cout << endl;
}
}
else
{
cout<<"The file isnt open.\n";
}
return 0;
}
Name 1, Name 2, Name 3
641610012, 1350, 17
659855655, 569, 89
142664525, 589, 122
I was thinking it might be beneficial to keep it. I was gonna try to reconvert it and use it again but they (names) also gets stored as zeros. Would the spaces in the names have something to do with that?
int main(){
double sumGCL, TPM, FPKM, sumCNTS;
string item;
ifstream myfile;
myfile.open("/Users/T_HEN1203/Desktop/DarkAerobic.csv");
//used to count the number of lines in the file
int numLines(0);
string unused;
while ( getline(myfile, unused) ){
++numLines;
}
//varibles to hold info from file
int Geneinfo[numLines][3];
int Gene_ID[numLines];
float Gene_length[numLines], Gene_RC[numLines];
if ( myfile.is_open() ){
// for loop for converting the strings into ints and storing them in a multidimensional array
for (int row = 1; row < numLines; ++row){
getline(myfile, item);
stringstream iss(item);
for (int col(0); col < 3; ++col){
string line;
getline(iss, line, ',');
// used to break loop if files stops being read
if ( !iss.good() ){
break;
}
stringstream convertor(line);
convertor >> Geneinfo[row][col];
}
}
// used to display the array
cout << "Gene ID " << "Length " << "Read Counts";
for (int r = 0; r < numLines; ++r){
for (int c = 0; c < 3; ++c){
cout << Geneinfo[r][c] <<" ";
}
cout << endl;
}
}
else{
cerr<<"ERROR: The file isnt open.\n";
}
return 0;
}
First array sizes in C++ must be compile time constants. If you want user sized arrays you should be using std::vector instead. The other alternative would be to use dynamic memory, new/delete.
1 2 3 4
//varibles to hold info from file
int Geneinfo[numLines][3];
int Gene_ID[numLines];
float Gene_length[numLines], Gene_RC[numLines];
Since numLines is not a compile time constant, all of the above lines should produce error messages.
Second your first loop reads to the end of the file which sets an error flag. Which means until you reset the error flag none of the rest of the file reads will succeed.