0

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;
};
  • Why don't you use `getline(displayf, some_string, '\n')` and then process that `some_string` the way you want to? –  Jan 12 '15 at 11:27
  • can you show the DataBlock structure – Ganesh Kamath - 'Code Frenzy' Jan 12 '15 at 11:32
  • On observation, the asterisks aren't numbers so they can't be put into an integer. I don't see any code that handles asterisks; I suggest using getline(..) placing each line into a string, and if a character within the string is an asterisk you should continue. – Poriferous Jan 12 '15 at 11:39
  • Thanks for your replies. Can you please explain exactly how to use getline. what changes are required. I am not good in file handling at all. – Muhammad Jan Sher Khan Jan 12 '15 at 11:45

2 Answers2

0

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.

Rodrigo Valente
  • 289
  • 1
  • 14
  • Thank you for the answer. I have tried this. The problem now is that all the data in file is integer and all data members of struct are also integers. I will be thankful if you can suggest a solution. – Muhammad Jan Sher Khan Jan 12 '15 at 11:52
  • Hey! Hello! I found this here in stack overflow about your problem, hope it can help you. [Read Numeric Data from a Text File in C++](http://stackoverflow.com/questions/14516915/read-numeric-data-from-a-text-file-in-c) – Rodrigo Valente Jan 12 '15 at 11:57
  • 1
    [Don't tell people to loop on `eof()`, it's a bad practice.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) Just do `while (getline(displayf, fileLine)) {}` instead. – Blastfurnace Jan 12 '15 at 11:58
  • The only problem is the asterisk in the file as they are not integers. Is there a way to skip them. Please see edits in the original question. Thanks – Muhammad Jan Sher Khan Jan 12 '15 at 12:27
  • Well, dont know if this is a good aproach... But you could read it first as a string, compare with "***". Then read as a int. – Rodrigo Valente Jan 12 '15 at 15:14
0

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