Thread: SegFault when reading a file

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about using fscanf to control your loop? It does have a return value you know.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you've completely trashed the stack with something.
    Are you sure things like char call[4]; are large enough. That's only 3 chars and a \0

    Code:
    char buff[BUFSIZ];
    char call[BUFSIZ];
    float lat;
    float lon;
    char name[BUFSIZ];
    while ( fgets( buff, sizeof buff, radar_file ) != NULL ) {
      if ( sscanf( buff, "%s %f %f %s",call,&lat,&lon,name ) == 4 ) {
      } else {
        // error in format of line
      }
    }
    Notice the feof() has gone, as per the FAQ we keep telling you to not bother to read.

    Using BUFSIZ for call may seem excessive, but I want to keep it simple, rather than having to explain the complicated syntax of limiting string length conversions in sscanf.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM