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

Recommended Answers

All 4 Replies

you have two functions with the name 'main', one in each file. Only one function 'main' can exist in the program. Change one of them.

And, as Naru will tell you if I don't, it should be "int main()" not "void main()".

Thank you for you help. Is there any way that I can combine the two .cpp files into one?

No need, just rename one of the mains to be something else.

Thank you for you help. Is there any way that I can combine the two .cpp files into one?

Just combine them in your editor. Put all the header files near the top and sort them out for duplicates. Also obvious things like #include "search.cpp" are not needed any more. Occasionally the order of the header files might play a role.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.