Alpha Mineron
Alpha Mineron

Reputation: 348

How to make Data File Handling functions in a private header file? (To be edited)

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

Upvotes: 0

Views: 52

Answers (1)

Jun Ge
Jun Ge

Reputation: 420

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;
    }
}

Upvotes: 0

Related Questions