Computing.Net > Forums > Programming > file handling in c++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

file handling in c++

Reply to Message Icon

Name: freefika
Date: August 1, 2003 at 10:53:55 Pacific
OS: win
CPU/Ram: 64
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Khash
Date: August 1, 2003 at 12:53:57 Pacific
Reply:

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


0

Response Number 2
Name: freefika
Date: August 2, 2003 at 00:02:07 Pacific
Reply:

isn't there any way to use this ios::app with the xyz.write function?


0

Response Number 3
Name: Khash
Date: August 2, 2003 at 08:54:15 Pacific
Reply:

I have never used xyz.write so I'm not sure.


0

Response Number 4
Name: freefika
Date: August 2, 2003 at 09:34:10 Pacific
Reply:

#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();
}


0

Response Number 5
Name: freefika
Date: August 2, 2003 at 09:35:48 Pacific
Reply:

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?


0

Related Posts

See More



Response Number 6
Name: waterdog
Date: August 2, 2003 at 10:05:07 Pacific
Reply:

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


0

Response Number 7
Name: igork
Date: August 2, 2003 at 10:41:11 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

simple sql trim question QBasic trouble...subrouti...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: file handling in c++

File Descriptors in C?? www.computing.net/answers/programming/file-descriptors-in-c/10512.html

writng files to folder in C++ www.computing.net/answers/programming/writng-files-to-folder-in-c/3485.html

Python file handling www.computing.net/answers/programming/python-file-handling/13036.html