Solved

files and classes

Posted on 2004-08-12
3
229 Views
Last Modified: 2012-05-05
Consider the following code ....
#include <fstream.h>
#include <stdio.h>
#include <conio.h>

class word
{
     char wrd[50];
public:
     void inpu()
     {
          cout<<"\n Enter the word ";
          gets(wrd);
     }
     void out()
     {
          cout<<"\n Word "<<wrd;
     }
};
void output()
{
     ifstream f;
     f.open("word.dat");
     word w;
     f.read((char*)&w,sizeof(w));
     while(!f.eof())
     {
           w.out();
           f.read((char*)&w,sizeof(w));
     }
     f.close();
}
void input()
{
     ofstream f;
     f.open("word.dat",ios::app);
     word w;
     w.inpu();
     f.write((char*)&w,sizeof(w));
     f.close();

}
void main()
{
     int ch;
     cout<<"\n 1. input 2.output \n enter the code ";
     cin>>ch;
     switch(ch)
     {
           case 1:
                  input();
                  break;
           case 2:
                  output();
                  break;
     }
     getche();
}


i enter the word car, bus, train in a file....

but the ouput function shows the following:

word car
word us

please tell me what is the problem
0
Comment
Question by:jebinabraham
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 6

Accepted Solution

by:
SCDMETA earned 20 total points
ID: 11788841
You seem to be using old c and C++.

Try using C++ includes:
  #include <fstream>
  #include <iostream>      
  using namespace std;
instead of
  #include <fstream.h>
  #include <stdio.h>
  #include <conio.h>

Use
  cin >> wrd;  
instead of  
  gets(wrd);

and get rid of
  getche();
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Here's a look at newsworthy articles and community happenings during the last month.
Simple Linear Regression
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question