Files in c++

R

reddy

Hai,

in my program i am creating two files, and displaying that files, but
i am getting some errors like, "name' : is not a member of 'fstream'"?
in that i am not getting some fields from what i created file ? i want
to display the some fields only with one condition, how , please give
the your answer? in below i am pasting complete code. please check in
main()
{
..............
case '5':
.............
}

thanks alot
ready
-----------------------------
// this part i need to correct
stu_file.seekg(0, ios::beg);
while(!stu_file.eof())
{
if(stu_file.get(ort) == "berlin" )
if(!stu_file.eof())
{
cout<<stu_file.name;
cout<<stu_file.studiengang;
cout<<stu_file.gesamtnote;
}
else
if(!stu_file.eof())
{
cout<<"Student Name:"<<stu_file.name;
cout<<"Student Studiengang:"<<stu_file.studiengang;
cout<<"Student Result:"<<stu_file.gesamtnote;
}
}
-------------------------------------------------
//my complete code
#include<iostream.h>
#include<fstream.h>
#include<string.h>
//using namespace std;

class PersonalDetails // creating Base calss
{
public:
//variables
char name[25];
char vorname[25];
char ort[25];

};
class Student : public PersonalDetails
//creating stdent class derived from PersonalDetails calss
{
public:
// variables for student
char matrikelnummer[25];
char studiengang[25];
char gesamtnote[25];
public:
Student() { }
//display to screen
Student( char *n, char *vn, char *or,char *matn)
{
strcpy(name, n);
strcpy(vorname, vn);
strcpy(ort, or);
strcpy(matrikelnummer, matn);
//strcpy(gesamtnote, "notcompleted");
}

friend istream &operator>>(istream &stream, Student &st);
friend ostream &operator<<(ostream &stream, Student st);
};
//Saving into file
ostream &operator<<(ostream &stream, Student st)
{
stream<<st.name <<"\n";
stream<<st.vorname <<"\n";
stream<<st.ort<<"\n";
stream<<st.matrikelnummer<<"\n";
stream<<strcpy(st.gesamtnote,"notcompleted")<<"\n";
return stream;
}
istream &operator>>(istream &stream, Student &st)
{

cout<<"enter the Student Name :";
stream>>st.name;
cout<<"enter the Student Vorname :";
stream>>st.vorname;
cout<<"enter Studnet Ort :";
stream>>st.ort;
cout<<"enter the Student Matrikelnummer :";
stream>> st.matrikelnummer;
cout<<"enter the Studiengang :";
stream>>st.studiengang;
//stream>>st.gesamtnote;
cout<<"/n";
return stream;

}
class Professor : public PersonalDetails
{
public:
// variables
char funktion[25];
public:
Professor() { }
Professor( char *pn, char *pvn, char *por,char *pfun)
{
strcpy(name, pn);
strcpy(vorname, pvn);
strcpy(ort, por);
strcpy(funktion, pfun);
}

//friend ostream &operator<<(ostream &stream, Student pr);
//friend istream &operator>>(istream &stream, Student &pr);
};

main()
{
Student stud;
Professor prof;
char c;

// this file for Student data
fstream stu_file("Student.txt", ios::in | ios::eek:ut|ios::app);
if(!stu_file)
{
cout<<" it is not posible to open a file";
return 1;
}
// this file for Professor
fstream pro_file("professor.txt", ios::in | ios::eek:ut |ios::app);
if(!pro_file)
{
cout<<"it is not posible to open Professer file";
return 1;
}
for(;;)
{
do
{
cout<<"1. for Enter Student Data\n";
cout<<"2. for Enter Professor Data\n";
cout<<"3. for Display the stednt details\n";
cout<<"4. for Display the Proffesor Details\n";
cout<<"5. for Who are commpleted \n";
cout<<"6. for Who are not completed \n";
cout<<"7. for Exit\n";
cout<<"\nEnter your choice";
cin>>c;
}while(c < '1' || c > '7');
switch(c)
{
case '1':
cin>>stud;
cout<<" Entry is:";
cout<<stud; // show on screen
stu_file << stud;// saveing in to the file
break;
/*case '2':
cin>>prof;
cout<<" Entry is:";
cout<<prof; // show on screen
pro_file << prof;// saveing in to the file
break;*/
case '3':
char ch;
stu_file.seekg(0, ios::beg);
while(!stu_file.eof())
{
stu_file.get (ch);
if(!stu_file.eof())
cout<<ch;
}
stu_file.clear();
//cout<< end1;
break;
/*case '4':
char ch1;
pro_file.seekg(0, ios::beg);
while(!pro_file.eof())
{
pro_file.get (ch1);
if(!pro_file.eof())
cout<<ch1;
}
pro_file.clear();
//cout<<end1;
break;*/
case '5':
stu_file.seekg(0, ios::beg);
while(!stu_file.eof())
{
if(stu_file.get(ort) == "berlin" )
if(!stu_file.eof())
{
cout<<stu_file.name;
cout<<stu_file.studiengang;
cout<<stu_file.gesamtnote;
}
else
if(!stu_file.eof())
{
cout<<"Student Name:"<<stu_file.name;
cout<<"Student Studiengang:"<<stu_file.studiengang;
cout<<"Student Result:"<<stu_file.gesamtnote;
}
}
stu_file.clear();
//cout<<end1;
break;
case '6':
break;
case '7':
stu_file.close();
pro_file.close();
return 0;
}
}
}
 
G

Gernot Frisch

Hi,

your stu_file is of type "fstream". You want to use variables of type
"Student", thus you should stream(load) the data from stu_file into a
Student structure and then display it's content.
I've fixed your code so it compiles now. I have not checked if it
work, but you see the point, hopefully.

Search for "GF:"

Code:
// Main
#include <windows.h>
#include<iostream.h>
#include<fstream.h>
#include<string.h>
//using namespace std;
class PersonalDetails // creating Base calss
{
public:
//variables
char name[25];
char vorname[25];
char ort[25];
};
class Student : public PersonalDetails
//creating stdent class derived from PersonalDetails calss
{
public:
// variables for student
char matrikelnummer[25];
char studiengang[25];
char gesamtnote[25];
public:
Student() { }
//display to screen
Student( char *n, char *vn, char *or,char *matn)
{
strcpy(name, n);
strcpy(vorname, vn);
strcpy(ort, or);
strcpy(matrikelnummer, matn);
//strcpy(gesamtnote, "notcompleted");
}
friend istream &operator>>(istream &stream, Student &st);
friend ostream &operator<<(ostream &stream, Student st);
};
//Saving into file
ostream &operator<<(ostream &stream, Student st)
{
stream<<st.name <<"\n";
stream<<st.vorname <<"\n";
stream<<st.ort<<"\n";
stream<<st.matrikelnummer<<"\n";
stream<<strcpy(st.gesamtnote,"notcompleted")<<"\n";
return stream;
}
istream &operator>>(istream &stream, Student &st)
{
cout<<"enter the Student Name :";
stream>>st.name;
cout<<"enter the Student Vorname :";
stream>>st.vorname;
cout<<"enter Studnet Ort :";
stream>>st.ort;
cout<<"enter the Student Matrikelnummer :";
stream>> st.matrikelnummer;
cout<<"enter the Studiengang :";
stream>>st.studiengang;
//stream>>st.gesamtnote;
cout<<"/n";
return stream;
}
class Professor : public PersonalDetails
{
public:
// variables
char funktion[25];
public:
Professor() { }
Professor( char *pn, char *pvn, char *por,char *pfun)
{
strcpy(name, pn);
strcpy(vorname, pvn);
strcpy(ort, por);
strcpy(funktion, pfun);
}
//friend ostream &operator<<(ostream &stream, Student pr);
//friend istream &operator>>(istream &stream, Student &pr);
};
main()
{
Student stud;
Professor prof;
char c;
// this file for Student data
fstream stu_file("Student.txt", ios::in | ios::eek:ut|ios::app);
if(!stu_file)
{
cout<<" it is not posible to open a file";
return 1;
}
// this file for Professor
fstream pro_file("professor.txt", ios::in | ios::eek:ut |ios::app);
if(!pro_file)
{
cout<<"it is not posible to open Professer file";
return 1;
}
for(;;)
{
do
{
cout<<"1. for Enter Student Data\n";
cout<<"2. for Enter Professor Data\n";
cout<<"3. for Display the stednt details\n";
cout<<"4. for Display the Proffesor Details\n";
cout<<"5. for Who are commpleted \n";
cout<<"6. for Who are not completed \n";
cout<<"7. for Exit\n";
cout<<"\nEnter your choice";
cin>>c;
}while(c < '1' || c > '7');
switch(c)
{
case '1':
cin>>stud;
cout<<" Entry is:";
cout<<stud; // show on screen
stu_file << stud;// saveing in to the file
break;
/*case '2':
cin>>prof;
cout<<" Entry is:";
cout<<prof; // show on screen
pro_file << prof;// saveing in to the file
break;*/
case '3':
char ch;
stu_file.seekg(0, ios::beg);
while(!stu_file.eof())
{
stu_file.get (ch);
if(!stu_file.eof())
cout<<ch;
}
stu_file.clear();
//cout<< end1;
break;
/*case '4':
char ch1;
pro_file.seekg(0, ios::beg);
while(!pro_file.eof())
{
pro_file.get (ch1);
if(!pro_file.eof())
cout<<ch1;
}
pro_file.clear();
//cout<<end1;
break;*/
case '5':
stu_file.seekg(0, ios::beg);
while(!stu_file.eof())
{
// GF: Temporäre Variable anlegen
Student depperl;
// GF: Daten von stu_file stream auf die Variable lesen
stu_file >> depperl;
// if(stu_file.get(ort) == "berlin" )
if(!stu_file.eof())
{
// GF: Variablendaten anzeigen.
cout<<depperl.name;
cout<<depperl.studiengang;
cout<<depperl.gesamtnote;
}
else
if(!stu_file.eof())
{
Student depperl;
stu_file >> depperl;

cout<<"Student Name:"<<depperl.name;
cout<<"Student Studiengang:"<<depperl.studiengang;
cout<<"Student Result:"<<depperl.gesamtnote;
}
}
stu_file.clear();
//cout<<end1;
break;
case '6':
break;
case '7':
stu_file.close();
pro_file.close();
return 0;
}
}
}
 
T

Thomas Matthews

reddy said:
Hai,

in my program i am creating two files, and displaying that files, but
i am getting some errors like, "name' : is not a member of 'fstream'"?
in that i am not getting some fields from what i created file ? i want
to display the some fields only with one condition, how , please give
the your answer? in below i am pasting complete code. please check in [snip]

//my complete code
#include<iostream.h>
#include<fstream.h>
#include<string.h>
The above headers are deprecated. Remove the '.h'.
//using namespace std;

class PersonalDetails // creating Base calss
{
public:
//variables
char name[25];
char vorname[25];
char ort[25];

};
Why are you using char arrays?
Use std::string class instead.
If you must use a fixed size char array, then use named
constants for the size:
class PersonalDetails
{
public:
enum {MAX_NAME_LENGTH = 25};
char name[MAX_NAME_LENGTH];
char vorname[MAX_NAME_LENGTH];
char ort[MAX_NAME_LENGTH];
}

Also, you should put in methods for input and output
of these values into this class.

class Student : public PersonalDetails
//creating stdent class derived from PersonalDetails calss
{
public:
// variables for student
char matrikelnummer[25];
char studiengang[25];
char gesamtnote[25];
See above comments about strings & named constants.

public:
Student() { }
//display to screen
Student( char *n, char *vn, char *or,char *matn)
{
strcpy(name, n);
strcpy(vorname, vn);
strcpy(ort, or);
strcpy(matrikelnummer, matn);
//strcpy(gesamtnote, "notcompleted");
}
With std::string, you could just use an initialization list:
Student(const string& n, const string& vn,
const string& or, const string& matn)
: PersonalDetails(n, vn, or, matn)
{
}

friend istream &operator>>(istream &stream, Student &st);
friend ostream &operator<<(ostream &stream, Student st);
These should also be defined in PersonalDetails.

};
//Saving into file
ostream &operator<<(ostream &stream, Student st)
{
stream<<st.name <<"\n";
stream<<st.vorname <<"\n";
stream<<st.ort<<"\n";
stream<<st.matrikelnummer<<"\n";
stream<<strcpy(st.gesamtnote,"notcompleted")<<"\n";
return stream;
}
istream &operator>>(istream &stream, Student &st)
{

cout<<"enter the Student Name :";
stream>>st.name;
cout<<"enter the Student Vorname :";
stream>>st.vorname;
cout<<"enter Studnet Ort :";
stream>>st.ort;
cout<<"enter the Student Matrikelnummer :";
stream>> st.matrikelnummer;
cout<<"enter the Studiengang :";
stream>>st.studiengang;
//stream>>st.gesamtnote;
cout<<"/n";
return stream;
How are you checking that the char arrays are not overflowed.
For example, if I enter a student name that is 30 chars long,
what will happen?

}
class Professor : public PersonalDetails
{
public:
// variables
char funktion[25];
public:
Professor() { }
Professor( char *pn, char *pvn, char *por,char *pfun)
{
strcpy(name, pn);
strcpy(vorname, pvn);
strcpy(ort, por);
strcpy(funktion, pfun);
}
Again, see note about std::string and initialization lists.

//friend ostream &operator<<(ostream &stream, Student pr);
//friend istream &operator>>(istream &stream, Student &pr);
};

main()
Close. This should be:
int main(void)

{
Student stud;
Professor prof;
char c;

// this file for Student data
fstream stu_file("Student.txt", ios::in | ios::eek:ut|ios::app);
if(!stu_file)
{
cout<<" it is not posible to open a file";
return 1;
Use EXIT_FAILURE, from <cstdlib>, instead of 1.
Also, state the file name in the output.

**** Note that stu_file is of type fstream. ******
**** This will help out later in the program. *****
}
// this file for Professor
fstream pro_file("professor.txt", ios::in | ios::eek:ut |ios::app);
if(!pro_file)
{
cout<<"it is not posible to open Professer file";
return 1;
Use EXIT_FAILURE, from <cstdlib>, instead of 1.
Also, state the file name in the output.

}
for(;;)
{
do
{
cout<<"1. for Enter Student Data\n";
cout<<"2. for Enter Professor Data\n";
cout<<"3. for Display the stednt details\n";
cout<<"4. for Display the Proffesor Details\n";
cout<<"5. for Who are commpleted \n";
cout<<"6. for Who are not completed \n";
cout<<"7. for Exit\n";
cout<<"\nEnter your choice";
cin>>c;
}while(c < '1' || c > '7');
You could use an unsigned int for the choice.

switch(c)
{ [snip]

case '5':
stu_file.seekg(0, ios::beg);
while(!stu_file.eof())
{
if(stu_file.get(ort) == "berlin" )
Where is the variable 'ort' defined?

if(!stu_file.eof())
{
cout<<stu_file.name;
cout<<stu_file.studiengang;
cout<<stu_file.gesamtnote;
As I stated above, "stu_file" is of fstream, not
Student. You will need to use your variable of
type 'Student' (stud) here.

}
else
if(!stu_file.eof())
{
cout<<"Student Name:"<<stu_file.name;
cout<<"Student Studiengang:"<<stu_file.studiengang;
cout<<"Student Result:"<<stu_file.gesamtnote;
}
The variable 'stu_file' is of type fstream (see the
'if' statement). Use 'stud' instead.

}
stu_file.clear();
//cout<<end1;
break;
case '6':
break;
case '7':
stu_file.close();
pro_file.close();
return 0;
}
}
}


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
M

Michiel Salters

Hai,

in my program i am creating two files, and displaying that files, but
i am getting some errors like, "name' : is not a member of 'fstream'"?
in that i am not getting some fields from what i created file ? i want
to display the some fields only with one condition, how , please give
the your answer? in below i am pasting complete code. please check in

Try writing a small program first. You added code for a professor
without testing the student code first. This complexity means you
can't find your own bugs.

The bug is in fact very simple. class fstream doesn't have a member
called name. class Student does. This means one of two things:
Your object stu_file should have had type Student, or
you should have used your stud object which has the correct type.

Regards,
Michiel Salters
 
D

Default User

Thomas said:
The above headers are deprecated. Remove the '.h'.


Not so fast. <string.h> is the only one deprecated, the others are
nonstandard. Also, <string.h> should be updated with <cstring> and not
<string>.




Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top