473,395 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Files in c++

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::out|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::out |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;
}
}
}
Jul 22 '05 #1
4 1359
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::out|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::out |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;
}
}
}
Jul 22 '05 #2
reddy wrote:
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::out|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::out |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

Jul 22 '05 #3
su***********@yahoo.com (reddy) wrote in message news:<3d**************************@posting.google. com>...
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
Jul 22 '05 #4
Thomas Matthews wrote:
//my complete code
#include<iostream.h>
#include<fstream.h>
#include<string.h>

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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
44
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
18
by: JKop | last post by:
Here's what I know so far: You have a C++ project. You have source files in it. When you go to compile it, first thing the preprocessor sticks the header files into each source file. So now...
3
by: pooja | last post by:
Suppose i have created a class c1 with f1()in c1.cpp and included this c1.cpp in file1.cpp file , which is also having main() by giving the statement #include "c1.cpp". the same i can do by...
11
by: ambika | last post by:
Iam just trying to know "c". And I have a small doubt about these header files. The header files just contain the declaration part...Where is the definition for these declarations written??And how...
22
by: Daniel Billingsley | last post by:
Ok, I wanted to ask this separate from nospam's ridiculous thread in hopes it could get some honest attention. VB6 had a some simple and fast mechanisms for retrieving values from basic text...
18
by: UJ | last post by:
Folks, We provide custom content for our customers. Currently we put the files on our server and people have a program we provide that will download the files. These files are usually SWF, HTML or...
0
by: wal | last post by:
How does one attach files to emails using libgmail? The following code http://pramode.net/articles/lfy/fuse/4.txt works fine when said files are simple text files, but it failes as soon as the...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.