Having issues with a .dat file

This is a discussion on Having issues with a .dat file within the C Programming forums, part of the General Programming Boards category; I am opening a Courses.dat file and need to read specific bytes on each line. For some reason, after opening ...

  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
    22,265
    Perhaps the error lies before this code snippet.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    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, 06:01 AM
  2. Resource file issues
    By xixpsychoxix in forum Windows Programming
    Replies: 9
    Last Post: 09-10-2009, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21