Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 16th, 2006, 8:26 AM   #1
jazz
Newbie
 
Join Date: May 2006
Posts: 18
Rep Power: 0 jazz is on a distinguished road
Segmentation Fault

Hi,

I have the below program which read a file and prints into an output file, It is compiling with out any errrors, but giving a segmentation fault while running it. Can anyone help??

  
   FILE     *fp_inFile;
   FILE    *fp_outFile;
   static int iGetSUNFile(FILE*, FILE*);
   static int iGetSTARFile(FILE*, FILE*);
   static int iGetMOONFile(FILE*, FILE*);
   char ctype;
   char caoutfile[50]; //output file
   /******* Sample main function ********/
   int main( int argc, char **argv ) {
               /* Checking for correct number of arguments */
               if((argc == NULL)||(argc > 3)||(argc < 3)) {
               printf("\tErr -> command line\n");
               printf("\tUSAGE : FilesDump Type Filename\n");
                           exit(NG);
     }
     else {
               printf("\tCommand accepted  -> Type :[%s] FileName :(%s)\n",argv[1],argv[2]);
     }
     /* fopen */
               if(NULL==(fp_inFile=fopen(argv[2],"rb"))) {
               printf("IN File Open Err! -> %s\n",argv[2]);
               exit(NG);
               }
     /* Copying the inputfile name and concatinating ".txt" to the output file */  
     strcpy(caoutfile, argv[2]);
     strcat(caoutfile,".txt");
     strcat(caoutfile, "\0");
     printf("\tNew FileName = %s\n",caoutfile);
               if ((fp_outFile=fopen(caoutfile, "w")) == NULL) {
               printf( "OutFile Open Error\n" );
               exit( NG );
               }
                  if(strcmp(argv[1],"SUN") == 0) {
                            ctype=0;
                  }
                            else if(strcmp(argv[1],"STAR") == 0) {
                              ctype=1;
                            }
                              else if(strcmp(argv[1],"MOON") == 0) {
                                        ctype=2;
                              }
               /* recognising the type of File Entered at command line */
               switch(ctype)
                 {
                 case 0 :
                           if(NULL == iGetSUNFile(fp_inFile, fp_outFile))
                             exit(NG);
                           break;
                          
                 case 1 :
                             if(NULL == iGetSTARFile(fp_inFile, fp_outFile))
                             exit(NG);
                           break;
    
                 case 2 :
                           if(NULL == iGetMOONFile(fp_inFile, fp_outFile))
                             exit(NG);
                           break;
    
                 default : printf("\tError -> Type must be one of the following – SUN, STAR, MOON \n");
                           break;
                 }
       exit( OK );
   }
   /*======================================================================
        FUNCTION     : Get SUN File
        RETURN       : OK / NG
   ======================================================================*/
   int iGetSUNFile(FILE* fp_inFile, FILE* fp_outFile)
   {
               int        i,j;
               unsigned long  ulRecordCount=0, ulDatabaseID=0, ulAlbumID=0, ulVolumeID=0;
               unsigned long  ulSectionID=0, ulLayerID=0, ulRecordID=0, ulNameRecordCount=0;
               unsigned short usNameType=0;
               unsigned char  ucLanguageCode;
               char           acString[40];
               fread((void *)ulRecordCount,8,1,fp_inFile);
               fprintf(fp_outFile, "Record Count : %lu\n", ulRecordCount);
               for(i = 0; i< ulRecordCount; i++){
                           fread((void *)ulDatabaseID, 8, 1, fp_inFile);
                           fread((void *)ulAlbumID, 8, 1, fp_inFile);
                           fread((void *)ulVolumeID, 8, 1, fp_inFile);
                           fread((void *)ulSectionID, 8, 1, fp_inFile);
                           fread((void *)ulLayerID, 8, 1, fp_inFile);
                           fread((void *)ulNameRecordCount, 8, 1, fp_inFile);
                           for(j =0; j<ulNameRecordCount; j++) {
                             fread((void *)ucLanguageCode, 1, 1, fp_inFile);
                             fread((void *)usNameType, 2, 1, fp_inFile);
                             fread((void *)acString, 40, 1, fp_inFile);
               fprintf(fp_outFile, "DBID:%lu\t AlbumID:%ul\t VolID:%lu\t SecID:%lu\t LayID:%lu\t \n NameRecordCount:%lu\n LangCode:%us\t NameType:%us\t String:%s\n", ulDatabaseID,ulAlbumID,ulVolumeID, ulSectionID,ulLayerID, ulNameRecordCount, ucLanguageCode, usNameType,acString);
                           }
               }
               fclose( fp_inFile);
               fclose(fp_outFile);
               return( OK );
   }
   /*======================================================================
        FUNCTION     : Get STAR File
        RETURN       : OK / NG
   ======================================================================*/
   int iGetSTARFile(FILE* fp_inFile, FILE* fp_outFile)
   {
               int        i;
               unsigned long  ulRecordCount=0, ulDatabaseID=0, ulAlbumID=0, ulVolumeID=0;
               unsigned long  ulDatasetID=0, ulSectionID=0, ulLayerID=0, ulRecordID=0;
               fread((void *)ulRecordCount,8,1,fp_inFile);
               fprintf(fp_outFile, "Record Count : %lu\n", ulRecordCount);
               for(i = 0; i< ulRecordCount; i++) {
                           fread((void *)ulDatabaseID, 8, 1, fp_inFile);
                           fread((void *)ulAlbumID, 8, 1, fp_inFile);
                           fread((void *)ulVolumeID, 8, 1, fp_inFile);
                           fread((void *)ulDatasetID, 8, 1, fp_inFile);
                           fread((void *)ulSectionID, 8, 1, fp_inFile);
                           fread((void *)ulLayerID, 8, 1, fp_inFile);
    
               fprintf(fp_outFile, "DBID:%lu\t AlbumID:%ul\t VolID:%lu\t DatasetID:%lu\t SecID:%lu\t LayID:%lu\t \n", ulDatabaseID,ulAlbumID,ulVolumeID,ulDatasetID, ulSectionID,ulLayerID);
               }
               fclose( fp_inFile);
               fclose(fp_outFile);
               return( OK );
   }
   /*======================================================================
        FUNCTION     : Get MOON File
        RETURN       : OK / NG
   ======================================================================*/
   int iGetMOONFile(FILE* fp_inFile, FILE* fp_outFile)
   {
               int        i;
               unsigned long  ulRecordCount=0, ulDatabaseID=0, ulAlbumID=0, ulVolumeID=0;
               unsigned long  ulDatasetID=0, ulSectionID=0, ulLayerID=0, ulRecordID=0;
               long    lLatitude=0, lLongitude=0, lAltitude=0;
               fread((void *)ulRecordCount, 8,1,fp_inFile);
               fprintf(fp_outFile, "Record Count : %lu\n", ulRecordCount);
               for(i = 0; i< ulRecordCount; i++) {
                           fread((void *)ulDatabaseID, 8, 1, fp_inFile);
                           fread((void *)ulAlbumID, 8, 1, fp_inFile);
                           fread((void *)ulVolumeID, 8, 1, fp_inFile);
                           fread((void *)ulDatasetID, 8, 1, fp_inFile);
                           fread((void *)ulSectionID, 8, 1, fp_inFile);
                           fread((void *)ulLayerID, 8, 1, fp_inFile);
                           fread((void *)lLatitude, 8, 1, fp_inFile);
                           fread((void *)lLongitude, 8, 1, fp_inFile);
                           fread((void *)lAltitude, 8, 1, fp_inFile);
    
               fprintf(fp_outFile, "DBID:%lu\t AlbumID:%lu\t VolID:%lu\t DatasetID:%lu\t SecID:%lu\t LayID:%lu\t \n Lat:%d\t Long:%d\t Alt:%d\n", ulDatabaseID,ulAlbumID,ulVolumeID,ulDatasetID, ulSectionID,ulLayerID,lLatitude,lLongitude,lAltitude);
               }
               fclose( fp_inFile);
               fclose(fp_outFile);
              
               return( OK );
   }
jazz is offline   Reply With Quote
Old Aug 16th, 2006, 9:04 AM   #2
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Run the program through a debugger, it will stop where the seg fault appears. This will greatly help you, and us fix the problem.
Game_Ender is offline   Reply With Quote
Old Aug 16th, 2006, 10:19 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Excellent suggestion. There's a good chance that you are putting more in a buffer than you have provided room for, or that you are referring to a non-existent buffer with a bunged-up pointer. Watch those things as you step through or run to a fault. These things can also be isolated with debug printfs, but using a debugger is much more effective. If you are using the debugger that comes with Dev-Cpp (as an example), you will have less visibility of some of these than if you have a more powerful kind. If you can't watch what you want, you can at least isolate the area of the failure and fall back on printfs to fill in the blanks. The salient point here: information is key. When you ask, give detailed error messages, line numbers, where the program SEEMS to be when the failure occurs, reasonable stuff like that. "While it's running" is a tad shy of sufficiency.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Aug 17th, 2006, 1:18 PM   #4
Kennedy
Newbie
 
Join Date: Aug 2006
Location: Alabama
Posts: 26
Rep Power: 0 Kennedy is on a distinguished road
I think that your problem may be here:
fread((void *)ulRecordCount,8,1,fp_inFile);

Suggest maybe &ulRecordCount.

Otherwise, ulRecordCount holds the location of memory your attempting to store to.
Kennedy is offline   Reply With Quote
Old Aug 17th, 2006, 2:07 PM   #5
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
It looks like all of your fread statements are like that. They should look like:
fread(&ulRecordCount, sizeof(unsigned long), 1, fp_inFile);

Like Kennedy said below you are telling fread to write a block of memory at address ulRecordCount, which is actually 0x0 (NULL), at this point. fread can only write into an already allocated buffer.

Not to mention that you should check the return value of those statements to make sure you are actually reading something from the file.
Game_Ender is offline   Reply With Quote
Old Aug 21st, 2006, 4:16 AM   #6
jazz
Newbie
 
Join Date: May 2006
Posts: 18
Rep Power: 0 jazz is on a distinguished road
Thankyou All.
jazz is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation Fault Cipher C++ 2 Feb 21st, 2006 3:10 PM
Segmentation fault rsnd C++ 12 Sep 24th, 2005 11:07 AM
segmentation fault on turing strings to uppercase Jessehk C++ 5 Aug 22nd, 2005 11:26 PM
segmentation fault caused by malloc to66 C 7 Apr 8th, 2005 2:44 PM
fprintf segmentation fault Michael Edgar C++ 0 Mar 21st, 2005 1:21 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:44 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC