You are here:

C++/UNABLE TO FETCH DATA FROM THE FILE ,WRITEN THROUGHT OBJECT

Advertisement


Question
sir,
   i am unable to compare the data enter by user and data in file writen through object, while runing function.

my program code as follow:-

#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
#include<conio.h>
#include<string.h>

class inventory
{
      public:   char name[10];
     int code;
     float cost;
         void readdata();
     void writedata();
     void check();
};

void inventory::readdata()
{
  cout<<"nenter name="; cin>>name;
  cout<<"nenter code="; cin>>code;
  cout<<"nenter cost"; cin>>cost;
}

void inventory:: writedata()
{
  cout<<"n"<<name;
  cout<<"tt"<<code;
  cout<<"tt"<<cost;

}

void inventory::check()
{
  inventory c;
  clrscr();
  ifstream fin;

  fin.open("stock.dat");

  char nm[10];
  cout<<"enter the name=";
  cin>>nm;

  fin.seekg(0);

  int flag=0;
  cout<<"noutputnn";
     
  while(fin && flag==0)
  {
         fin.read((char*)&c,sizeof(c));
         
         //in condition statement i want to comare user enter data(nm(char)).
         //with the data in file which is an object variable(name).
         //if it found the name variable of oject in a file then it will proceed further.       
         
         if(strcmp(name,nm)==0)
         {
        writedata();
        flag=1;
         }

  }
     
  if(flag==0)
     cout<<"data does not exits in file";


  fin.close();

getch();

}


void main()
{
  inventory item;
  //inventory b;
  
       clrscr();
  ofstream fout;
  fout.open("stock.dat");

  cout<<"Enter details for itemnn";
  item.readdata();
  
  fout.write((char*)&item,sizeof(item));

  fout.close();
     
  item.check();

getch();
}


NOTE:-
problem 2:-
*after writing the data i the file it is runing if we call the funtion check().

*after executing the program there is garbage value in the file "stock.dat".

*the file containing the data and then if we run only check() function  by comenting the code of writing in file. then it will not compare.


i will be  highly thankful to you.if you help me out.  

Answer
Hello,

The strcmp needs to compare c.name to nm.

You could use fin.read((char *)this, sizeof *this);

and then your strcmp will work.

The file has garbage as seen in an editor, but I assure you, the data in the file is as written.  Note because you wrote a block of data in binary, the file may or may not be portable and read on another computer.

Hope this helps you finish!
Bill

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bill A

Expertise

I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.

Experience

I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Publications
Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

Education/Credentials
BS Computer Engineering

©2016 About.com. All rights reserved.