Thread: fopen error & Assertion error - fgets.c

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    Question fopen error & Assertion error - fgets.c

    Hi there,

    Firstly i hope this is the right board to post on, secondly apologies as im not an experianced programmer so may appear a little ignorant!

    Ive written a 2D type sprite game in visual c++ in microsoft visual studio .NET which works fine within the Debug menu. Howerver i get my error when i try and run the .exe made for my game.

    heres the error message

    Code:
    Debug Assertion Failed!
    
    program: basejoystick.exe
    File: fgets.c
    Line: 60
    
    Expression: str !=NULL
    So my first error is due to not opening the file correctly i think, which gives me an error with the fgets function in my code. I worked this out by putting in the message box which tells me the target file isnt loaded.

    Code:
         char	TheTargetFileNumber[15];
         char c = '\n';
         char fileInfo[50];
         char filename[50];
         
         strcpy(filename, "static_data1.csv"); //write the string of the filename to char filename
         staticData_f = fopen(filename, "r"); // open the file here
    
         if(staticData_f == NULL)
    	{	MessageBox( NULL, TEXT("target file tracker.\n")\					       TEXT("not found\n")\
    		       TEXT("."),  
    		       TEXT("."), MB_ICONINFORMATION | MB_OK );
    		return FALSE;
    	}   // this message box will tell me if file not found
    
         fgets(fileInfo, sizeof(fileInfo), staticData_f);
         sscanf(fileInfo, "%15s", &TheTargetFileNumber);
         fclose(staticData_f);
    in my file static_data1.csv i have this string

    Code:
    targets001.csv
    I have my static_data1.csv file in the same directory as other files im opening and closing fine.

    I would be greatfull if anyone could help me!! In other parts of my programe i open and close files in the same way with no problems?

    Many thanks,
    ANDY

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if(staticData_f == NULL)
    If this is true, then use either
    perror() to print a message on stderr, indicating why it failed
    strerror() to get a char* pointer to a message indicating why it failed.

    > targets001.csv
    You do know this only just fits inside your buffer?

    > sscanf(fileInfo, "%15s", &TheTargetFileNumber);
    Unlike fgets, the field width in a scanf string doesn't make allowances for a \0, so this call has the capability of a 1 char overflow.
    Also, you don't need the & when referring to char arrays.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    Hi Salem,

    Thanks for the speedy reply!! Thats a good idea with perror(), i guess i do something like this? (taken from MSDN libary)

    Code:
    int main( void )
    {
       int  fh;
    
       if( _sopen_s( &fh, "NOSUCHF.ILE", _O_RDONLY, _SH_DENYNO, 0 ) != 0 )
       {
          /* Three ways to create error message: */
          perror( "perror says open failed" );
          printf( "strerror says open failed: %s\n",
             strerror( errno ) ); // C4996
          printf( _strerror( "_strerror says open failed" ) ); // C4996
          // Note: strerror and _strerror are deprecated; consider
          // using strerror_s and _strerror_s instead.
       }
       else
       {
          printf( "open succeeded on input file\n" );
          _close( fh );
       }
    }

    >> targets001.csv
    >You do know this only just fits inside your buffer?

    Yes thanks, basically i use this string written to file to keep track of which target file i want to open for my game. It can be any number from 001 to 100, then it loops back, so the string will never be larger than this.

    >> sscanf(fileInfo, "%15s", &TheTargetFileNumber);
    >Unlike fgets, the field width in a scanf string doesn't make allowances for a \0, so this call has >the capability of a 1 char overflow.
    >Also, you don't need the & when referring to char arrays.

    right okay. I guess this wont be a problem if my char TheTargetFileNumber[15] has 15 elements, good to know! Thanks for the & advice.

    Ill have a look at the weekend and see if i make any progress.
    Many thanks again!!

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    Wink

    Hi guys,

    Solved the problem! I hadn't put my targetfile tracking .csv file into my debug directory. Thanks for the advice, i should put the file opening checking code into my program anyways!

    Many thanks,
    Andy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File I/O Assertion Failure in VS2008
    By clegs in forum C Programming
    Replies: 5
    Last Post: 12-25-2008, 04:47 AM
  2. Debug Assertion failure problem
    By uldaman in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2008, 02:22 AM
  3. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  4. fopen help.
    By chriscolden in forum C Programming
    Replies: 17
    Last Post: 01-13-2006, 06:27 AM
  5. Assertion Failure
    By drdroid in forum Game Programming
    Replies: 9
    Last Post: 01-04-2003, 07:02 PM