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]);
}