943,441 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3948
  • C++ RSS
Feb 1st, 2005
0

Error measage in compiling c++ program

Expand Post »
error reads: body has already been defined for function 'main()'
program is as follows:
search.h file

C++ Syntax (Toggle Plain Text)
  1. // display files in a directory
  2. #include <stdlib.h>
  3. #include <dir.h>
  4. #include <dos.h>
  5. #include <string.h>
  6. #include <iostream.h>
  7. #include <fstream.h>
  8.  
  9. ofstream outfile("output.dat",ios::out);
  10.  
  11. class dispfiles
  12. {
  13. public:
  14. int showfiles(void);
  15. char allfiles[MAXPATH];
  16. private:
  17. char *getdirectory(char *path);
  18. char curdir[MAXPATH],temp[3];
  19. struct ffblk fileinfo;
  20. int attrib,done;
  21. };

search.cpp file

C++ Syntax (Toggle Plain Text)
  1. /*
  2. * Routine to hunt down a snippet of text inside
  3. * a file
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #define MAXCHAR 2048 //number of characters to read from disk
  11.  
  12. void main(void)
  13. {
  14. char filename[256];
  15. char text[256];
  16. long x;
  17. char buffer[MAXCHAR+1];
  18. FILE *f;
  19. long matches = 0;
  20. char *a, *b;
  21.  
  22.  
  23. strcpy(filename, "c:\\textfile.txt"); // Get filename
  24. strupr(filename); //convert filename to U/C
  25.  
  26. if((f = fopen(filename, "r")) == NULL)
  27. {
  28. printf("Error opening %s\n", filename);
  29. exit(0);
  30. }
  31. //Get search string
  32. strcpy(text,"COMPUTER");
  33.  
  34. printf("\nLooking in %s for \"%s\" . . . \n",filename,text);
  35.  
  36. printf(">> %s found\n",filename);
  37. printf(">> Hunting for \%s\"\n",text);
  38.  
  39. /* Scan for a matching byte */
  40.  
  41. x = 1; //File offset
  42.  
  43. while(fgets(buffer,MAXCHAR,f))
  44. {
  45. b = buffer;
  46. if( (a = strstr(buffer,text)) != NULL)
  47. {
  48. matches++; //inc. match count
  49. printf("Found at offset %Lu\n",x+a-b);
  50. }
  51. x+=strlen(buffer); //adjust offset
  52. }
  53.  
  54. printf("\nFound a total of %Lu matches\n",matches);
  55. fclose(f);
  56. }

main.cpp program
C++ Syntax (Toggle Plain Text)
  1. #include "search.cpp"
  2. #include "search.h"
  3.  
  4. void main()
  5. {
  6. dispfiles fileobj;
  7. char rootdir[MAXPATH];
  8.  
  9. rootdir[0] = '@' + 3;
  10. rootdir[1] = ':';
  11. rootdir[2] = 92;
  12. rootdir[3] = 0;
  13. setdisk(2);
  14. chdir(rootdir);
  15. strcpy(fileobj.allfiles,"*.exe");
  16. fileobj.showfiles();
  17. outfile.close();
  18. }
  19.  
  20.  
  21. char *dispfiles::getdirectory(char *path)
  22. {
  23. strcpy(path,"X:\\");
  24. path[0] = 'A' + getdisk();
  25. getcurdir(0,path+3);
  26. return(path);
  27. }
  28.  
  29. int dispfiles::showfiles(void)
  30. {
  31. attrib = 47;
  32. getdirectory(curdir);
  33. done = findfirst(allfiles,&fileinfo,attrib);
  34. if(!done) outfile << endl << "Path: " << curdir << endl;
  35. while(!done)
  36. {
  37. outfile << fileinfo.ff_name << endl;
  38. done = findnext(&fileinfo);
  39. }
  40. attrib = FA_DIREC;
  41. done = findfirst("*.*",&fileinfo,attrib);
  42. while(!done)
  43. {
  44. strncpy(temp,fileinfo.ff_name,2);
  45. if(((fileinfo.ff_attrib & FA_DIREC) == FA_DIREC) && (temp[0] != '.'))
  46. {
  47. if(strlen(curdir) !=3)strcat(curdir,"\\");
  48. strcat(curdir,fileinfo.ff_name);
  49. chdir(curdir);
  50. done = showfiles();
  51. chdir("..");
  52. getdirectory(curdir);
  53. }
  54. done = findnext(&fileinfo);
  55. }
  56. return done;
  57. }
Last edited by alc6379; Feb 1st, 2005 at 10:57 am. Reason: added [code] tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wildkms725 is offline Offline
2 posts
since Feb 2005
Feb 1st, 2005
0

Re: Error measage in compiling c++ program

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()".
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Feb 1st, 2005
0

Re: Error measage in compiling c++ program

Thank you for you help. Is there any way that I can combine the two .cpp files into one?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
wildkms725 is offline Offline
2 posts
since Feb 2005
Feb 2nd, 2005
0

Re: Error measage in compiling c++ program

No need, just rename one of the mains to be something else.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Feb 2nd, 2005
0

Re: Error measage in compiling c++ program

Quote originally posted by wildkms725 ...
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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Does C++ indubitably have a standard?
Next Thread in C++ Forum Timeline: Little help Comparing Strings





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC