0

EDIT: This question is to be edited, please stop reading. Don't waste your time! Thank you

I’m doing High School Turbo C++. I tried making a header file containing a function to search a binary file. My Headerfile program is: alpha.h

#ifndef ALPHA_H
#define ALPHA_H

#if !defined __FSTREAM_H
#include<fstream.h>
#endif

#if !defined __PROCESS_H
#include<process.h>
#endif

void searchclass(char* & buf, int, char *);

#endif

According to some research that I did on the internet, I found out that the definitions will go in a separate program not in the main header file. So this is that program: ALPHA.CPP

#include<process.h>
#include<fstream.h>
#include"alpha.h"

//All the Definations of the alpha.h header file go here

void searchclass(char* & buf, int s_var, char * file)
{
    ifstream fin;
    fin.open(file, ios::binary);
    if(!fin)
    {
    cout<<"Error 404: File not found";
    exit(-1);
    }
    while(!fin.read((char*)buf, sizeof(buf)))
    if(buf.getint()==s_var)
        cout<<"\n\nRecord Found!";
        buf.show();
    fin.close();
}

Keep in mind that I'm trying to write a function that can help me search a random binary file storing records in form of classes for some specific int variable. So it should be able to take in an object of any class and perform a search in it.

This is the program I wrote to check my header file. A_TEST.CPP

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include"alpha.h"
#include<string.h>

class stu
{   int rn;
    char name[20];
public:
void show()   //Display Function
{
   cout<<"\n\tStudent Details:";
   cout<<"\nName: "<<name;
   cout<<"\nRoll No.: "<<rn;
}
stu()       //constructor
{
  rn = 6;
  strcpy(name,"Random Name");
}
int getint()            //return function
{ 
  return rn; 
}
};

char* returnfile()
{ char file[10];
  strcpy(file,"file.dat");
  return file;
}

void main()
{
    clrscr();
    int search_var=6;
    stu S1;
    char file[10];
    strcpy(file, "test.dat");
    ofstream fout;
    fout.open(file, ios::binary);
    fout.write((char*)&S1, sizeof(S1));
    fout.close();

    searchclass((char*)& S1, search_var, file);

    getch();
}

On compiling A_TEST.CPP (above program), I get the warning:

Warning A_TEST.CPP 45: Temporary used for parameter 'buf' in call to 'searchclass(char * &,int,char *)'

On linking, it gives me this error:

Linking A_TEST.EXE

Linker Error: Undefined symbol searchclass(char nearnear&,int,char near) in module A_TEST.CPP

I don't think that the ALPHA.CPP file is getting linked with the alpha.h file, and if I compile ALPHA.CPP file it gives me the following errors:

Error ALPHA.CPP 17: Structure required on left side of . or .*

Error ALPHA.CPP 19: Structure required on left side of . or .*

Warning ALPHA.CPP 21: Parameter 's_var' is never used

Community
  • 1
  • 1
Alpha Mineron
  • 348
  • 1
  • 2
  • 13

1 Answers1

0

In ALPHA.CPP 17 and 19, you can't write code like that, because int type hasn't getint() or show() methods. If you want to check int in the buffer, you should convert pointer into "int*" first, try below code:

int count = fin.read((char*)buf, sizeof(buf));
for (int i = 0; i<(count-4); ++i) {
    if (*(int *)(buf + i) == s_var) {
        cout << "\n\nRecord Found!";
        break;
    }
}
Jun Ge
  • 408
  • 4
  • 13
  • Okay but, How would I use the getint() and show() functions? I mean since it's a class, the data members are hidden in private. Also, I'm a bit confused on how to substitute your code in mine. What I'm trying to do is: make searchclass() function work in any program where the alpha.h file is included. So the class can be anything(it doesn't have to be _class stu_. So, I was trying to pass the object of a class in form of a buffer...... This haven't been taught in class so I was just trying to use the prototype of write and read functions in the iostream. So, I might be completely wrong – Alpha Mineron Apr 02 '17 at 10:21
  • Your code gives an error: "Cannot convert istream into int". How do I fix it? Please respond – Alpha Mineron Jul 07 '17 at 15:57