I am trying to extract data from file and put it into a dynamic array of type DataBlock (DataBlock is struct). The problem is that data is extracted correctly in the first iteration of the for loop but not the afterwards. I have debugged, the problem is that pointer is not moving to next lines after first iteration of for loop and assigning the last value of the first iteration to the whole array.
edit: Now I realized that the problem is due to " **** " in file as these are not integers. But they are necessary(they help to calculate records size) Is there a way to skip the lines with " **** " . ?
Here is the data in file:
3
4
2029
23
45
459
***
1
2
2015
3
4
20
***
When data is moved to array and displayed, it shows:
3
4
2029
23
45
459
***
459
459
459
459
459
459
***
Here is the code:
DataBlock *writetoarray(string uname)
{
int size;
int data;
size = SizeOfArray(uname);
cout << "size:" << size << endl;
DataBlock *ptr = new DataBlock[size];
ifstream displayf;
displayf.open(uname.c_str());
int i = 0;
for (int i = 0; i < size; i++)
{
displayf >> data;
//istream& getline(displayf >> data);
ptr[i].date = data;
cout << ptr[i].date << endl;
displayf >> data;
ptr[i].month = data;
cout << ptr[i].month << endl;
displayf >> data;
ptr[i].year = data;
cout << ptr[i].year << endl;
displayf >> data;
ptr[i].hrs = data;
cout << ptr[i].hrs << endl;
displayf >> data;
ptr[i].min = data;
cout << ptr[i].min << endl;
displayf >> data;
ptr[i].bgl = data;
cout << ptr[i].bgl << endl;
displayf >> data;
cout << "***" << endl;
}
displayf.close();
return ptr;
}//function ends
I have searched a lot but couldn't find the solution. Tomorrow is the submission date. I will be very thankful if you could help me out. Please do tell me if any further information is required.
adding more details: Here is the DataBlock code:
struct DataBlock
{
int date, month, year, hrs, min, bgl;
};