Solved

files and classes

Posted on 2004-08-12
3
219 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

808 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