error reads: body has already been defined for function 'main()'
program is as follows:
search.h file
// display files in a directory
#include <stdlib.h>
#include <dir.h>
#include <dos.h>
#include <string.h>
#include <iostream.h>
#include <fstream.h>
ofstream outfile("output.dat",ios::out);
class dispfiles
{
public:
int showfiles(void);
char allfiles[MAXPATH];
private:
char *getdirectory(char *path);
char curdir[MAXPATH],temp[3];
struct ffblk fileinfo;
int attrib,done;
};
search.cpp file
/*
* Routine to hunt down a snippet of text inside
* a file
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHAR 2048 //number of characters to read from disk
void main(void)
{
char filename[256];
char text[256];
long x;
char buffer[MAXCHAR+1];
FILE *f;
long matches = 0;
char *a, *b;
strcpy(filename, "c:\\textfile.txt"); // Get filename
strupr(filename); //convert filename to U/C
if((f = fopen(filename, "r")) == NULL)
{
printf("Error opening %s\n", filename);
exit(0);
}
//Get search string
strcpy(text,"COMPUTER");
printf("\nLooking in %s for \"%s\" . . . \n",filename,text);
printf(">> %s found\n",filename);
printf(">> Hunting for \%s\"\n",text);
/* Scan for a matching byte */
x = 1; //File offset
while(fgets(buffer,MAXCHAR,f))
{
b = buffer;
if( (a = strstr(buffer,text)) != NULL)
{
matches++; //inc. match count
printf("Found at offset %Lu\n",x+a-b);
}
x+=strlen(buffer); //adjust offset
}
printf("\nFound a total of %Lu matches\n",matches);
fclose(f);
}
main.cpp program
#include "search.cpp"
#include "search.h"
void main()
{
dispfiles fileobj;
char rootdir[MAXPATH];
rootdir[0] = '@' + 3;
rootdir[1] = ':';
rootdir[2] = 92;
rootdir[3] = 0;
setdisk(2);
chdir(rootdir);
strcpy(fileobj.allfiles,"*.exe");
fileobj.showfiles();
outfile.close();
}
char *dispfiles::getdirectory(char *path)
{
strcpy(path,"X:\\");
path[0] = 'A' + getdisk();
getcurdir(0,path+3);
return(path);
}
int dispfiles::showfiles(void)
{
attrib = 47;
getdirectory(curdir);
done = findfirst(allfiles,&fileinfo,attrib);
if(!done) outfile << endl << "Path: " << curdir << endl;
while(!done)
{
outfile << fileinfo.ff_name << endl;
done = findnext(&fileinfo);
}
attrib = FA_DIREC;
done = findfirst("*.*",&fileinfo,attrib);
while(!done)
{
strncpy(temp,fileinfo.ff_name,2);
if(((fileinfo.ff_attrib & FA_DIREC) == FA_DIREC) && (temp[0] != '.'))
{
if(strlen(curdir) !=3)strcat(curdir,"\\");
strcat(curdir,fileinfo.ff_name);
chdir(curdir);
done = showfiles();
chdir("..");
getdirectory(curdir);
}
done = findnext(&fileinfo);
}
return done;
}