GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Feb-2006, 21:50
vermin1302 vermin1302 is offline
New Member
 
Join Date: Feb 2006
Location: Indiana, USA
Posts: 19
vermin1302 is on a distinguished road

segmentation fault error


Ok, this code compiles just fine, but once I run the program and move into this section of it, I get a segmentation fault. Since I'm not really much of a programmer (this is the first thing I've written in five years), I'm not quite sure what's going wrong.

Here's the snippet that's giving me trouble:

CPP / C++ / C Code:
    else {
    #ifndef _WIN32
    system("/usr/bin/clear");
    #else
    system("cls");
    #endif
    
    int old_turn;
    int old_ps;
    int old_pa;
    int old_cs;
    int old_ca;
    
    printf("Displaying formatted contents of dilemma.log.\n\n");
    printf("| TURNS | PSCORE | PAVG | CSCORE | CAVG |\n");
    printf("|-------+--------+------+--------+------+\n");
    
    while(fscanf(pFile,"%3d %3d %4f %3d %4f",old_turn,old_ps,old_pa,old_cs,old_ca) != EOF)
    {
         printf("|  %03d  |  %03d   | %4.2f |  %03d   | %4.2f |\n",old_turn,old_ps,old_pa,old_cs,old_ca);
         printf("|-------+--------+------+--------+------+\n");
    }
    
    printf("\nEnd of file reached.\n");
    printf("Press any key to return.\n");
    tempvar = getch();
    
    break;
    }

Here's the information from the error message:

AppName: dilemma.exe AppVer: 3.2.0.9 ModName: ntdll.dll
ModVer: 5.1.2600.2180 Offset: 00001010

Thanks a bunch for any help!
  #2  
Old 24-Feb-2006, 22:45
Chris.Dev's Avatar
Chris.Dev Chris.Dev is offline
Junior Member
 
Join Date: Jan 2005
Posts: 48
Chris.Dev will become famous soon enough

Re: segmentation fault error


The most likely reason for the segmentation fault is demonstrated bellow.

Try compiling and running this code:
CPP / C++ / C Code:
#include <stdio.h>

/* ^^^^^ im assuming this is C? ^^^^^ */

int main(void) {
    int i1, i2, i3, i4;        /* some integers to be read from FILE *fp */
    FILE *fp;                /* input file pointer */


    fp = NULL;              /* fp = NULL?! UhOh, a stack dump waiting to
                                    happen!
                                */
                              
    /* ready for the segmentation fault? */
    fscanf(fp, "%d%d%d%d", i1, i2, i3, i4);
    /************************************/
    
    return 0;
}

Passing a NULL pointer to a standard library function is never a good idea, and will never give the results you want.

simple solution:
CPP / C++ / C Code:

    if(pFile == NULL) {
        printf("ERROR: pFile is an invalid file pointer: %p !\n", pFile);
        break;        /* or whatever else you want to do */
    }


The real problem here however is that your file pointer was never assigned a value:

fopen return value:
Quote:
Originally Posted by cplusplus.com
Return Value:
If the file has been succesfully opened the function will return a pointer to the file. Otherwise a NULL pointer is returned.

If your program hadn't crashed it still wouldn't have worked right anyway so make sure you are accessing a valid file.

One more thing...

Quote:
Originally Posted by vermin1302
#ifndef _WIN32
system("/usr/bin/clear");
#else
system("cls");
#endif

Is that really what you wanted to do?

cls is for windows
clear is for *nix
  #3  
Old 24-Feb-2006, 23:16
vermin1302 vermin1302 is offline
New Member
 
Join Date: Feb 2006
Location: Indiana, USA
Posts: 19
vermin1302 is on a distinguished road

Re: segmentation fault error


CPP / C++ / C Code:
#ifndef _WIN32
system("/usr/bin/clear");
#else
system("cls");
#endif

That actually says...if it's NOT a windows machine, use clear.
If it is, use cls.

But, as far as the suggestion you gave...maybe it would help if I added a larger sample of that problematic code snippet here:

CPP / C++ / C Code:
case 'V':                             // player wants to view logs
    {
    pFile = fopen("dilemma.log","r");
    
    if(pFile=NULL) {
    perror("Error opening dilemma.log");
    printf("\nCheck that it exists and try again.\n");
    printf("Press any key to continue.\n");
    tempvar = getch();
    
    break;
    }
    else {
    #ifndef _WIN32
    system("/usr/bin/clear");
    #else
    system("cls");
    #endif
    
    int old_turn;
    int old_ps;
    int old_pa;
    int old_cs;
    int old_ca;
    
    printf("Displaying formatted contents of dilemma.log.\n\n");
    printf("| TURNS | PSCORE | PAVG | CSCORE | CAVG |\n");
    printf("|-------+--------+------+--------+------+\n");
    
    while(fscanf(pFile,"%3d %3d %4f %3d %4f",old_turn,old_ps,old_pa,old_cs,old_ca) != EOF)
    {
         printf("|  %03d  |  %03d   | %4.2f |  %03d   | %4.2f |\n",old_turn,old_ps,old_pa,old_cs,old_ca);
         printf("|-------+--------+------+--------+------+\n");
    }
    
    printf("\nEnd of file reached.\n");
    printf("Press any key to return.\n");
    tempvar = getch();
    
    break;
    }
    break;
    }

maybe that'll make the problem easier to solve.

As you can see, I did use fopen() to assign a value to my file pointer (pFile), and I also did account for a NULL pointer.

The heading there in the else statement (right before the while) will print out and then the segmentation fault happens right afterward.
  #4  
Old 25-Feb-2006, 00:58
TreyAU21's Avatar
TreyAU21 TreyAU21 is offline
Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 116
TreyAU21 has a spectacular aura aboutTreyAU21 has a spectacular aura about

Re: segmentation fault error


fscanf(FILE * stream , const char * format [ , argument , ...] );

You aren't using pointers as your arguments in fscanf().

Here is your code:

CPP / C++ / C Code:
while(fscanf(pFile,"%3d %3d %4f %3d %4f",old_turn,old_ps,old_pa,old_cs,old_ca) != EOF)

add the & sign in front of the variables in fscanf to reference the address of the variables...

CPP / C++ / C Code:
while(fscanf(pFile,"%3d %3d %4f %3d %4f",&old_turn,&old_ps,&old_pa,&old_cs,&old_ca) != EOF)


That should fix it I think.
__________________
If practice makes perfect and nobody's perfect... why practice?

Homepage: http://www.treywhite.com
Blog: http://www.treywhite.com/blog.php
Web Design Company: http://www.ewebproductions.com
  #5  
Old 25-Feb-2006, 01:29
Chris.Dev's Avatar
Chris.Dev Chris.Dev is offline
Junior Member
 
Join Date: Jan 2005
Posts: 48
Chris.Dev will become famous soon enough

Re: segmentation fault error


Quote:
Originally Posted by vermin1302
CPP / C++ / C Code:
#ifndef _WIN32
system("/usr/bin/clear");
#else
system("cls");
#endif

My bad, sorry it's real late here and im munching caffeine pills working on a project

I read it as #ifdef, I guess I need some sleep

Anyway TreyAU21 is right, you need to pass your variables by referance.
I also overlooked that :-/

This situation should highlight the reasons not to use scanf or it's dirivitives
  #6  
Old 25-Feb-2006, 11:45
vermin1302 vermin1302 is offline
New Member
 
Join Date: Feb 2006
Location: Indiana, USA
Posts: 19
vermin1302 is on a distinguished road

Re: segmentation fault error


Thanks again, but that still didn't fix the problem.

I changed the while line so it now reads
CPP / C++ / C Code:
    while(fscanf(pFile,"%3d %3d %4f %3d %4f",&old_turn,&old_ps,&old_pa,&old_cs,&old_ca) != EOF)

Compiles fine again, gets to the same spot and faults.
  #7  
Old 25-Feb-2006, 11:48
vermin1302 vermin1302 is offline
New Member
 
Join Date: Feb 2006
Location: Indiana, USA
Posts: 19
vermin1302 is on a distinguished road

Re: segmentation fault error


oooh, looks like I found the source of the problem.

I had
CPP / C++ / C Code:
if(pFile = NULL)

When I changed it to
CPP / C++ / C Code:
if(pFile == NULL)
, it stopped faulting.

However, now the actuall log output is messed up.

When I run the program, here's what I get:
Quote:
Displaying formatted contents of dilemma.log.

| TURNS | PSCORE | PAVG | CSCORE | CAVG |
|-------+--------+------+--------+------+
| 90 | 270 | 0.00 | 1077936128 | 0.00 |
|-------+--------+------+--------+------+
| 54 | 162 | 0.00 | 1077936128 | 0.00 |
|-------+--------+------+--------+------+

End of file reached.
Press any key to return.

The contents of the logfile are:

90 270 3.00 270 3.00
54 162 3.00 162 3.00
  #8  
Old 25-Feb-2006, 13:38
vermin1302 vermin1302 is offline
New Member
 
Join Date: Feb 2006
Location: Indiana, USA
Posts: 19
vermin1302 is on a distinguished road

Re: segmentation fault error


Ok, I figured out what was wrong. I declared old_ca and old_pa as int instead of float. It's all fixed and working fine now.

Thanks a lot for your help!
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB 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
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 15:41
Winsock error when compiling FLTK 2.0 Projects mauriciorossi FLTK Forum 3 16-Aug-2005 10:18
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 20:30
What is "Ambigious symbol" ??*( a compilation error) small_ticket C++ Forum 2 07-Jan-2005 21:10
Can enum have same name as class? crystalattice C++ Forum 3 08-Dec-2004 16:43

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:19.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.