vendredi 13 mars 2015

not reading the first line [on hold]


I was using the following code to read information from a file (b.txt) into a vector. every time I execute, the program starts reading from the second line. So vector_b[0] = the second line instead of the first line.


I'm still not used to c++ hope you can help



string line_b;
int idx = -1;
float *vector_b = new float[1];
float element_b;
if (file_b.is_open() && file_b.good()) {
cout << "File is open. \n";
while (getline(file_b, line_b)) {
stringstream stream_b(line_b);
while(1) {
stream_b >> element_b;
if(!stream_b) {
break; }
idx = idx+1;
float *tempArr = new float [idx];
copy(vector_b, vector_b + idx, tempArr);
tempArr[idx] = element_b;
delete [] vector_b;
vector_b = tempArr;
}
}
}
else {
cout << "Failed to open file.";
}



Aucun commentaire:

Enregistrer un commentaire