Thread: Program crashing at fgets() - any ideas why?

  1. #1
    Registered User
    Join Date
    Feb 2009

    Program crashing at fgets() - any ideas why?

    Hi all,

    As a part of my program, I am opening a file and reading it line by line in a loop and processing the lines.

    But my program keep crashing when I try to run it..
     for(j=0; j< (*anntn)[i].num_peaks; j++)
               printf("Read line %d\n",j);
               fgets(szbuf, 1500,fp);
               mz_i= (int) (mz_f +0.5);
               if( (*lib_spec)[i][mz_i] < intensity)
     	     (*lib_spec)[i][mz_i] = intensity;
               printf("Finished read of line %d\n",j);
            fgets(szbuf, 1500,fp); /** Skipping the blank line after the end of 1 record **/
    This is the part of the code which is giving me the problem.. The loop runs exactly 14 times and then crashes. I tried putting the printf statement at different places to see where exactly it crashes. It crashes immediately after calling the fgets function.

    When I googled for crashes at fgets(), most of the results talked about crashes because of trying to open a file which was opened before and wasn't closed. I only open this file once. And besides, since the first 14 lines are read correctly, I dont think the problem is because of problems with opening the file.

    I thought it could be a problem with the size of szbuf.. But i increased the size 10 times and the program still crashes at the same place.

    I am out of ideas. Does any one have any suggestions on what I should try..?


  2. #2
    Registered User
    Join Date
    Sep 2006
    sscanf() I'd remove the \t from the sscanf() call. Use two calls, and between them, add the \t into the buffer, if you want. Mind you, the buffer won't know anything about what '\t' is, however. It's purpose in your code, eludes me frankly.

    I would then rem out the other lines of code, and see how it works with the first few lines, if other troubleshooting is needed. You need to find the offending code, and that's a good way to quickly do it.

    You can stare at this stuff sometimes until you're cross-eyed, and still not see a simple error.

    Are you sure you're not at the end of the file?
    Last edited by Adak; 03-05-2009 at 06:46 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Without all the other declarations, especially all those arrays you're indexing through, it's really hard to say.

    Using printf to find a segfault is unreliable. Use a debugger.
    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.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Most importantly, we'd need to know what szbuf is declared as - I suspect it may be the problem.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2005
    You've not mentioned whether you've been testing the return value for fgets(). If there's a read error it should return a null pointer. I'd probably start off by checking that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashing when I use IO
    By legit in forum C++ Programming
    Replies: 9
    Last Post: 05-31-2009, 07:54 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Maze Program, Any ideas
    By drkavngr1911 in forum C++ Programming
    Replies: 14
    Last Post: 11-13-2003, 03:32 PM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. Program ideas...
    By code987 in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2002, 11:41 PM