[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

files and classes

Posted on 2004-08-12
3
Medium Priority
?
240 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 80 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
A bad practice commonly found during an account life cycle is to set its password to an initial, insecure password. The Password Reset Tool was developed to make the password reset process easier and more secure.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Progress
Suggested Courses

656 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