![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2005
Posts: 14
Rep Power: 0
![]() |
using classes, & fstream
hi . i am trying to output to a file using ofstream .. i'm down to 3 errors now : 'out' uses undefined class 'std::basic_ofstream<_Elem,_Traits>' also 'initializing' : cannot convert from 'const char [7]' to 'int' and error C2228: left of '.write' must have class/struct/union.
any help would be appreciated. the code is below: #include<fstream.h>
#include "stdafx.h"
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class Person
{
private:
int idNum;
char name[10];
public:
Person(const int id, const char nm[]);
friend ostream& operator<<(ostream& out,const Person &p);
friend istream& operator>>(istream& in, Person &p);
ostream& personDisplay(ostream &s);
};
Person::Person(const int id = 000, const char nm[]="")
{
idNum = id;
strcpy(name,nm);
}
ostream& operator<<(ostream& out,const Person &p)
{
out<<p.name<<p.idNum<<endl;
return(out);
}
ostream& Person::personDisplay(ostream &s)
{
cout<<"Here is a person: ";
cout.setf(ios::left);
cout.width(12);
return(s);
}
istream& operator>>(istream& in, Person &p)
{
cout<<"Enter ID Number ";
cin>>p.idNum;
cout<<"Enter name ";
cin>>p.name;
return(in);
}
void main()
{
Person aPerson;
ofstream out("person.dat"); // change path if necessary
cin>>aPerson;
out.write((char*)(&aPerson), sizeof(aPerson));
} |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Feb 2006
Posts: 20
Rep Power: 0
![]() |
Change your #include files to C++ standard ones, Also change void main() to int main(). Finally, You can remove the stdafx.h if you like. You don't need that header since you can turn precompiled headers off in MS Visual C++
The following compiled with 0 errors in MSVC++ 2003 .NET #include<fstream>
#include<iostream>
#include<conio.h>
#include<cstring>
using namespace std;
class Person
{
private:
int idNum;
char name[10];
public:
Person(const int id, const char nm[]);
friend ostream& operator<<(ostream& out,const Person &p);
friend istream& operator>>(istream& in, Person &p);
ostream& personDisplay(ostream &s);
};
Person::Person(const int id = 000, const char nm[]="")
{
idNum = id;
strcpy(name,nm);
}
ostream& operator<<(ostream& out,const Person &p)
{
out<<p.name<<p.idNum<<endl;
return(out);
}
ostream& Person::personDisplay(ostream &s)
{
cout<<"Here is a person: ";
cout.setf(ios::left);
cout.width(12);
return(s);
}
istream& operator>>(istream& in, Person &p)
{
cout<<"Enter ID Number ";
cin>>p.idNum;
cout<<"Enter name ";
cin>>p.name;
return(in);
}
int main()
{
Person aPerson;
ofstream out("person.dat"); // change path if necessary
cin>>aPerson;
out.write((char*)(&aPerson), sizeof(aPerson));
}After-thought: you could have made that code a little simpler if you'd used the C++ <string> library ![]() Last edited by Bench; Mar 28th, 2006 at 8:53 PM. Reason: Wrong include file |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Dec 2005
Posts: 14
Rep Power: 0
![]() |
Hey Bench
thank u so much .. really appreciate it .. u have no idea thanx! |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
You'll want the cconio header instead of conio.h too.
|
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
I'm not sure, but I don't think there is a cconio header as conio.h is not a standard C header file.
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Nice guess, though
.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Dec 2005
Posts: 14
Rep Power: 0
![]() |
cool ..
ok i want to print the results on the screen and i am getting an error and/or "16" do u guys know what code i should use ... i tried : int main()
{
Person aPerson;
ofstream out("person.dat");
cin>>aPerson;
cout<<((char*)(&aPerson), sizeof(aPerson)); //or
aPerson.personDisplay;
out.write((char*)(&aPerson), sizeof(aPerson));
getch();
} |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Dec 2005
Posts: 14
Rep Power: 0
![]() |
its cool .. it was a no-brainer.. figured it out
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|