| | |
Error measage in compiling c++ program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 2
Reputation:
Solved Threads: 0
error reads: body has already been defined for function 'main()'
program is as follows:
search.h file
search.cpp file
main.cpp program
program is as follows:
search.h file
C++ Syntax (Toggle Plain Text)
// 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
C++ Syntax (Toggle Plain Text)
/* * 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
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by alc6379; Feb 1st, 2005 at 10:57 am. Reason: added [code] tags
•
•
•
•
Originally Posted by wildkms725
Thank you for you help. Is there any way that I can combine the two .cpp files into one?
May 'the Google' be with you!
![]() |
Similar Threads
- kevelop:problem compiling first program (C++)
- Wierd error messages with calculator program (C++)
- error message when compiling program (C++)
- Error message when I run my program in C++ (C++)
Other Threads in the C++ Forum
- Previous Thread: Does C++ indubitably have a standard?
- Next Thread: Little help Comparing Strings
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






