简体   繁体   中英

Extracting line by line data from file into dynamic array in C++

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;
};

For file iteration is better to use the getline function. Try the following and see if it helps you:

 std::string fileLine;

 while(!displayf.eof()) //Will navegate in the file until it reachs the end.
 {
      getline(displayf, fileLine);

      //Now your file data is in fileLine variable

     /*Do what your need with the data here*/

 }

For each interation of the while loop, fileLine will have an line of your file, the loop will end autmatically when the file reach its end.

The problem got solved in this way:

It were the asterisks which were making problem as they are not int but the variable for extracting line is of int type

adding this just after extracting stars solved the problem:

displayf >> data;
        if (displayf.failbit)
        {
            cout << "***" << endl;
            displayf.clear();
            displayf.ignore(numeric_limits<streamsize>::max(), '\n');
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM