Thread: Having issues with a .dat file

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    15

    Having issues with a .dat file

    I am opening a Courses.dat file and need to read specific bytes on each line. For some reason, after opening the file, I get a segmentation fault on ANYTHING I do to it. I have tried opening with "r" and "rb", but nothing works. This might not be enough code to determine the error but I am just wondering what the possible cause could be for a seg fault immediately after opening a file.

    I have checked that the file exists, the file name is correct, I can open and close the file successfully, but as soon as I do anything with it, it fails.

    Code:
        if((datP = fopen(fname, "r")) == NULL) {              /*open .dat file*/
          fprintf(stderr,"Error opening .dat file.\n");   exit(1);
        }
      fseek(datP,0L,SEEK_END);
      fileLen = ftell(datP);
      fseek(datP,0L,SEEK_SET);
      printf("fileLen: %d",fileLen);

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps the error lies before this code snippet.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You should post a complete program that has the segfault if you want help. We basically have to debug to find the problem.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    As usual, I spend 30min trying to figure out whats going wrong. Then I post something and find the error 5 minutes later. It turns out the error was about 15 lines after this code. But since it wasnt printing out any of the output in between it was throwing me off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. issues reading from a file
    By sanddune008 in forum C Programming
    Replies: 3
    Last Post: 07-16-2010, 05:01 AM
  2. Resource file issues
    By xixpsychoxix in forum Windows Programming
    Replies: 9
    Last Post: 09-10-2009, 12:05 AM
  3. Having issues with indexing correctly in a file
    By McFury in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2008, 10:55 AM
  4. file saving issues
    By _lazycat_ in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2007, 12:22 AM
  5. Random File Issues
    By johnnyd in forum C Programming
    Replies: 5
    Last Post: 03-28-2002, 01:15 PM