the last record is getting displayed twice.i hv rechecked everything and cudnt find the error. here is the add n display function.

void add()
   example abc;
   int n;
   ofstream file1("EXAMPLE.dat", ios::binary|ios::app);
   cout<<"Enter Number Of Records To Add ";

   for ( int i = 0; i < n;  ++i)
      file1.write((char*)&abc, sizeof(abc));

void display()
   example abc;
   ifstream file("EXAMPLE.dat", ios::binary);

   {*)&abc, sizeof(abc));


kindly help.
wid regards

Recommended Answers

All 5 Replies

The problem is so simple you'll laugh...

Your loop says:

while there is no file failure (eof):
    abc <-- read next record
    display abc

What happens is this:
Read record one (no file read failure), display record one.
Read record two (no file read failure), display record two.
Read record N (no file read failure), display record N.
Read record N+1 (couldn't: file read failure), display record N (since abc == last record)
While loop terminates.

You need to test the file failure before displaying the record. There are a zillion ways to rewrite this, so I offer my way:

while (true)
  if (! ... )) break;
  std::cout << std::endl;

Hope this helps.

sorry wats this std::cout....???
i dont know this...!!!

You used cout yourself in your original program. Somewhere at the beginning of your code you probably have a line like

using namespace std;

which teachers persist in teaching people.

The cout object belongs to the std namespace, which you make active with the using statement. I typed it explicitly just to show good coding practice...


bt i dont hv
using namespace std;
at d beginning...i olwayz write progam widout dat n mah teacher never told me to write dat..
n thanx dat program worked....yippieeee..!!!

>bt i dont hv
>using namespace std;
>at d beginning...
If you're not using namespaces or getting compilation errors then you're not learning standard C++. I'd wager your teacher is teaching you C++ on his favorite compiler, which happens to be old as dirt.

>at d beginning...i olwayz write progam widout dat n mah teacher never told me to write dat..
Would you please write proper English? Not everyone is well versed in your personal flavor of crappy abbreviations and poor grammar.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.