Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
another problem! im creating the database of a college in c++. it contains records of students. im using file handling for this purpose. i write the data on the file using xyz.write and i used xyz.read to read the data, but if i enter over 1 record, and then read the data i have stored, it simply shows me the last record i entered. i suppose the data i enter gets over written. there is an append mode for this purpose, to add to existing file and not over write it. it looks something like this: "ios::app", but where exactly in the program do i use this?
plz help, URGENT

Yeah if you simply write to a file you delete the prev data and only write the new stuff. you have to let the computer know that you want to add to it when you open the file.
add one of these as the secong argument of fout() and you get what you want.fout(name.eg, ios::app) will append to the end of that file.
fout(name.eg, ios::ate) will go the the end of the file but you can add data to anywhere on the file.have fun

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<fstream.h>
struct x
{
int id;
float gpa;
};
x s;
ofstream abc("C:\\my documents\\data.dat");
void iff(void)
{
ifstream xyz("C:\\my documents\\data.dat");
while(xyz.read((char*)&s,sizeof(s)))
{
cout<<"ID:";
cout<<s.id;
cout<<"GPA:";
cout<<s.gpa;
}
xyz.close();
}
void of(void)
{
cout<<"ID:";
cin>>s.id;
cout<<"GPA:";
cin>>s.gpa;
abc.write((char*)&s,sizeof(s));
abc.close();
}
void intro(void)
{
int choice;
cin>>choice;
if(choice==1)
{
of();
intro();
}
else
{
if(choice==2)
{
iff();
intro();
}
}
}
void main(void)
{
intro();
getch();
}

in the above program, where do i go wrong? for example i enter 2 records, and then when i choose my choice to b 2, it will only show me the last entered record, not the whole thing...how do i fix it?

I'm not familiar with xyz.write either.
It looks like you only write or read to the first record in your file. You may have to add code to advance to the next record.
Something like -
abc.write((char*)&s,(sizeof(s)*recnumber));

Hi.
The problem is that u r treating THE SAME FILE as input and output AT THE SAME TIME via 2 DIFFERENT objects: abc and xyz.YOU CANNOT OPEN THE SAME FILE TWICE WITHOUT CLOSING IT BEFORE SECOND "OPEN" OPERATION !!!
I would advise you to use one instance of type : fstream. this way u can controll both input and output to the same file via 1 fstraem instance. Another important feature of fstream is
seekpos(.....)
method. You can use this method in order to
set the appropriate (IN/OUT) stream pointer to
the record u want to access, and then u can
read() or write() that record...Just read more about fstream methods - maybe
u `ll find more useful to u information

![]() |
simple sql trim question
|
QBasic trouble...subrouti...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |