Thread: Segmentation fault while acceesing .dat file in Unix.

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    4

    Segmentation fault while acceesing .dat file in Unix.

    Hi All,

    Below is my sample code of a C program. I am doing lot of processing, but my issue is I am getting a segmentation fault error in my main when I am putting the file names at the command line and using them. The program works great in Windows, but its giving error in Unix. Could you please help me with the same.

    Below is the code of my Main function.

    Code:
    int main(int argc, char *argv[])
    {
        
        FILE *user, *requests;
        char c;
        user = fopen(argv[1], "r");
        requests = fopen(argv[2], "r");
        
    
    
        //For users
    
    
        struct userStorage *USHead = NULL;
        //struct users *userHead = NULL;
    
    
        char originalLine[128];
        char *processingLine;
        char processingL[128];
    
    
        int uid;
        int processTime;
    
    
    
    
        //For jobs
    
    
        
        //struct jobs *jobHead = NULL;
    
    
        char jobLine[128];
        char *jobProcesslingLine;
        char *jobProcessingLine2;
        char *jobProcesslingLine3;
        char jobPL[128];
        char jobPL2[128];
        char jobPL3[128];
    
    
        int jid;
        int jpriority;
        int jtime;
    
    
        //For tempList;
    
    
    struct tempList *tempL = NULL;
    
    
    //Loop to handle users
    
    
    if (user != NULL)
    {
        char line[128];
    
    
        while (fgets(line, sizeof line, user) != NULL)
        {
            strcpy(originalLine, line);
            strtok_s(originalLine, ", ", &processingLine);
            strcpy(processingL, processingLine);
            uid = atoi(originalLine);
            processTime = atoi(processingL);
            //fputs(originalLine, stdout);
            //FOR EVERY LINE MAKE A USER NODE AND USER STORAGE NODE
    
    
            struct users *u = addUser(uid, processTime);
            addUserStorage(&USHead, &u);
    
    
        }
        fclose(user);
    }
    else
    {
        perror(argv[1]);
    }
    
    
    //Loop to handle requests
    
    
    if (requests != NULL)
    {
        char line[128];
    
    
        while (fgets(line, sizeof line, requests) != NULL)
        {
            strcpy(jobLine, line);
            strtok_s(jobLine, ", ", &jobProcesslingLine);
            uid = atoi(jobLine);
            strcpy(jobPL, jobProcesslingLine);
            strtok_s(jobPL, ", ", &jobProcessingLine2);
            jid = atoi(jobPL);
            strcpy(jobPL2, jobProcessingLine2);
            strtok_s(jobPL2, ", ", &jobProcesslingLine3);
            jpriority = atoi(jobPL2);
            strcpy(jobPL3, jobProcesslingLine3);
            jtime = atoi(jobPL3);
            //printf("User id is %d job id is %d job priority is %d processing time is %d\n", uid, jid, jpriority, jtime);
    
    
    
    
            //FOR EVERY REQUEST,ADD THE REQUEST IN THE TEMP LIST
    
    
            addTemp(&tempL, uid, jid, jpriority, jtime);
    
    
        }
        fclose(user);
    
    
    }
    else
    {
        perror(argv[2]);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How exactly are you calling your program?

    Have you run the program with your debugger? The debugger should be able to tell you where it detects the problem and you should be able to view the variables at the time of the crash.


    Jim

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    4
    Hi Jim,

    I am running the program through terminal. The issue is, I am getting the error file not found, even though the files are there.
    Which debugger should I try, can you suggest something

    Thanks,
    Tanmay

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Tanmay View Post
    Hi Jim,

    I am running the program through terminal. The issue is, I am getting the error file not found, even though the files are there.
    Which debugger should I try, can you suggest something

    Thanks,
    Tanmay
    Quote Originally Posted by jimblumberg View Post
    How exactly are you calling your program?
    Jim
    The above normally means what are you entering on the command line to call your program.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    4
    Hi,

    I am entering the file names on the command line.

    ./Main (object file) user.dat request.dat


    Thanks and Regards,
    Tanmay

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You should be using something like "./yourProgramName user.dat request.dat" where "yourProgramName" is the name of your program. You must be in the directory where your program executable is located and the data files must be in the same directory.

    Jim

  7. #7
    Registered User
    Join Date
    Oct 2014
    Posts
    4
    Hey, Thanks for the help Jim, Tim.

    I figured out my silly mistake.
    I was closing the user file after closing it in the first place.
    That was giving the segmentation core dump error.
    Got the code running now.

    Thanks,
    Tanmay

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault in Header file
    By Marc Oleson in forum C Programming
    Replies: 12
    Last Post: 04-26-2013, 05:39 PM
  2. Segmentation Fault. When trying to open file.
    By guyle in forum C Programming
    Replies: 5
    Last Post: 02-25-2013, 12:55 PM
  3. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  4. Segmentation fault with a file pointer?
    By Matt13 in forum C Programming
    Replies: 14
    Last Post: 07-31-2004, 05:53 AM
  5. Segmentation fault - C on a Unix m/c
    By Jimbo in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 04:37 PM