Solved

files and classes

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now