I had segmentation fault when I run it. I have input file in the same folder whe
ID: 3851290 • Letter: I
Question
I had segmentation fault when I run it.
I have input file in the same folder where the code exist.
when I compiled the code, there was no error found.
(The command I used is ./a.out input.txt output.txt)
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
//Define constant variable
#define MAXLINES 100
#define MAXNAMES 30
#define TRUE 1
#define NOTTRUE 0
//Declare functions
int countLinesInFile(FILE* fPtr);
int findPlayerByName(char** plNames, char* plTarget, int plSize);
int findMVP(int* plGoals, int* plAssists, int plSize);
void printPlayers(int* plGoals, int* plAssists, char** plNames, int plSize);
void allocateMemory(int** plGoals, int** plAssists, char*** plNames, int plSize);
void sortPlayersByGoals(int* plGoals, int* plAssists, char** plNames, int plSize);
void writeToFile(FILE* fPtr, int* plGoals, int* plAssists, char** plNames, int plSize);
void readLinesFromFile(FILE* fPtr, int* plGoals, int* plAssists, char** plNames, int numLines);
void bonusSortPlayersByBoth(int* plGoals, int* plAssists, char** plNames, int plSize);
//Main
int main(int argc, char** argv)
{
//Create new instance
FILE* fPtrIn;
FILE* fPtrOut;
//Check condition
if(argc != 3)
{
//Display
printf("Two and only two command line arguments are allowed, terminating. ");
//Exit
return 1;
}
//Open file given as command line arguments
fPtrIn = fopen(argv[1], "r");
fPtrOut = fopen(argv[2], "w");
//Check condition
if(fPtrIn == NULL || fPtrOut == NULL)
{
//Display
printf("Could not find both files, terminating. ");
//Exit
return 1;
}
//Declare the needed variables
char** plNames;
int* plGoals;
int* plAssists;
int idxMVP, idxTarget;
//Function call
int plSize = countLinesInFile(fPtrIn);
//Function call
allocateMemory(&plGoals, &plAssists,& plNames, plSize);
//Function call
readLinesFromFile(fPtrIn, plGoals, plAssists, plNames, plSize);
//Display
printf(" Read the following %d players from the file.. ", plSize);
//Function call
printPlayers(plGoals, plAssists, plNames, plSize);
//Function call
bonusSortPlayersByBoth(plGoals, plAssists, plNames, plSize);
//Display
printf(" ------ Bonus ------ ");
//Function call
printPlayers(plGoals, plAssists, plNames, plSize);
//Function call
idxMVP = findMVP(plGoals, plAssists, plSize);
//Display
printf(" MVP was %s with %d points ", *(plNames+idxMVP), (*(plGoals+idxMVP)+*(plAssists+idxMVP)));
//Allocate
char* tPlayer = (char*)malloc(MAXNAMES*sizeof(char));
//Prompt the user for input
printf("Enter a player to find: ");
//Read input
scanf("%s", tPlayer);
//Function call
idxTarget = findPlayerByName(plNames, tPlayer, plSize);
//Check condition
if(idxTarget != -1)
{
//Display
printf("%s has %d goals and %d assists ", *(plNames+idxTarget), *(plGoals+idxTarget), *(plAssists+idxTarget));
}
//Otherwise
else
{
//Display
printf("Error, player not found ");
}
//Function call
writeToFile(fPtrOut, plGoals, plAssists, plNames, plSize);
//Free memory
free(tPlayer);
free(plGoals);
free(plAssists);
//Close file
fclose(fPtrIn);
fclose(fPtrOut);
//Stop
return 0;
}
//Function countLinesInFile()
int countLinesInFile(FILE* fPtr)
{
//Initialize
int looper = TRUE, totaLine = 0;
//Allocate
char* lineCountString =(char*) malloc(MAXLINES*sizeof(char));
//Loop
while(looper == TRUE)
{
//Check condition
if(fgets(lineCountString, MAXLINES, fPtr) != NULL)
{
//Increment
totaLine++;
}
//Otherwise
else
{
//Update
looper = NOTTRUE;
}
}
//Free
rewind(fPtr);
free(lineCountString);
//Return
return totaLine;
}
//Function findPlayerByName()
int findPlayerByName(char** plNames, char* plTarget, int plSize)
{
//Variable
int idx1;
//Loop
for(idx1=0; idx1 {
//Check condition
if(strcmp(plTarget, *(plNames+idx1))==0)
{
//Return
return idx1;
}
}
//Return
return -1;
}
//Function findMVP
int findMVP(int* plGoals, int* plAssists, int plSize)
{
//Allocate
int* totalPoints = (int*)malloc(plSize*sizeof(int));
//Declare variables
int idx1, curMVP;
int curMax = 0;
//Loop
for(idx1=0; idx1 {
*(totalPoints+idx1) = *(plGoals+idx1) + *(plAssists+idx1);
//Check condition
if(*(totalPoints+idx1)> curMax)
{
//Update
curMax = *(totalPoints+idx1);
curMVP = idx1;
}
}
//Return
return curMVP;
//Free
free(totalPoints);
}
//Function printPlayers()
void printPlayers(int* plGoals, int* plAssists, char** plNames, int plSize)
{
//Display
printf("Name Goals Assists ");
//Variable
int idx1;
//Loop
for(idx1=0; idx1 {
//Display
printf("%-14s%-8d%d ", *(plNames+idx1), *(plGoals+idx1), *(plAssists+idx1));
}
}
//Function allocateMemory()
void allocateMemory(int** plGoals, int** plAssists, char*** plNames, int plSize)
{
//Variable
int idx1;
//Allocate
*plGoals =(int*) malloc(plSize*sizeof(int));
*plAssists =(int*) malloc(plSize*sizeof(int));
*plNames = (char**)malloc(plSize*sizeof(char*));
//Loop
for(idx1=0; idx1 {
//Update
*(*plNames+idx1) = (char*)malloc(MAXNAMES*sizeof(char));
}
}
//Function sortPlayersByGoals()
void sortPlayersByGoals(int* plGoals, int* plAssists, char** plNames, int plSize)
{
//Variables
int idx1, idx2, currMinimum, tAGoals, tAAssists;
//Allocate
char* tName = (char*)malloc(MAXNAMES*sizeof(char));
//Loop
for(idx1=0; idx1 {
//Update
currMinimum = idx1;
//Loop
for(idx2=idx1+1; idx2 {
//Check condition
if(*(plGoals+idx2)>*(plGoals+currMinimum))
{
//Update
currMinimum = idx2;
}
}
//Check condition
if(currMinimum != idx1)
{
//Update
tAGoals = *(plGoals+idx1);
tAAssists = *(plAssists+idx1);
strcpy(tName, *(plNames+idx1));
*(plGoals+idx1) = *(plGoals+currMinimum);
*(plAssists+idx1) = *(plAssists+currMinimum);
strcpy(*(plNames+idx1), *(plNames+currMinimum));
*(plGoals+currMinimum) = tAGoals;
*(plAssists+currMinimum) = tAAssists;
strcpy(*(plNames+currMinimum), tName);
}
}
//Free memory
free(tName);
}
//Function writeToFile()
void writeToFile(FILE* fPtr, int* plGoals, int* plAssists, char** plNames, int plSize)
{
//Variable
int idx1;
//Loop
for(idx1=0; idx1 {
//File write
fprintf(fPtr, "%s %d %d ", *(plNames+idx1), *(plGoals+idx1), *(plAssists+idx1));
}
rewind(fPtr);
}
//Function readLinesFromFile()
void readLinesFromFile(FILE* fPtr, int* plGoals, int* plAssists, char** plNames, int numLines)
{
//Variable
int idx1;
//Allocate
char* line = (char*)malloc(MAXLINES*sizeof(char));
char* goalString = (char*)malloc(MAXNAMES*sizeof(char));
char* assistsString = (char*)malloc(MAXNAMES*sizeof(char));
//Loop
for(idx1=0; idx1 {
//File get
fgets(line, MAXLINES, fPtr);
//String strtok
strtok(line, " ");
//String copy
strcpy(*(plNames+idx1), line);
//Update
goalString = strtok(NULL, " ");
assistsString = strtok(NULL, " ");
*(plGoals+idx1) = atoi(goalString);
*(plAssists+idx1) = atoi(assistsString);
}
//Free
free(line);
rewind(fPtr);
}
//Function bonusSortPlayersByBoth()
void bonusSortPlayersByBoth(int* plGoals, int* plAssists, char** plNames, int plSize)
{
//Function call
sortPlayersByGoals(plGoals, plAssists, plNames, plSize);
//Display
printf(" Sorted player list.. ");
//Function call
printPlayers(plGoals, plAssists, plNames, plSize);
//Variables
int idx1, idx2, currMinimum, tAGoals, tAAssists;
//Allocate
char* tName = (char*)malloc(MAXNAMES*sizeof(char));
//Loop
for(idx1=0; idx1 {
//Update
currMinimum = idx1;
//Loop
for(idx2=idx1+1; idx2 {
//Check condition
if(*(plGoals+idx2)>=*(plGoals+currMinimum))
{
//Check condition
if(*(plAssists+idx2)>*(plAssists+currMinimum))
{
//Update
currMinimum = idx2;
}
}
}
//Check condition
if(currMinimum != idx1)
{
//Update
tAGoals = *(plGoals+idx1);
tAAssists = *(plAssists+idx1);
strcpy(tName, *(plNames+idx1));
*(plGoals+idx1) = *(plGoals+currMinimum);
*(plAssists+idx1) = *(plAssists+currMinimum);
strcpy(*(plNames+idx1), *(plNames+currMinimum));
*(plGoals+currMinimum) = tAGoals;
*(plAssists+currMinimum) = tAAssists;
strcpy(*(plNames+currMinimum), tName);
}
}
//Free
free(tName);
}
Explanation / Answer
Can you please test this code and let me know if you still face segmentation fault ?
Tested with input file... Please take a look at the output and tell me after what point of output are you facing segmentation fault (if still present)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Define constant variable
#define MAXLINES 100
#define MAXNAMES 30
#define TRUE 1
#define NOTTRUE 0
//Declare functions
int countLinesInFile(FILE* fPtr);
int findPlayerByName(char** plNames, char* plTarget, int plSize);
int findMVP(int* plGoals, int* plAssists, int plSize);
void printPlayers(int* plGoals, int* plAssists, char** plNames, int plSize);
void allocateMemory(int** plGoals, int** plAssists, char*** plNames, int plSize);
void sortPlayersByGoals(int* plGoals, int* plAssists, char** plNames, int plSize);
void writeToFile(FILE* fPtr, int* plGoals, int* plAssists, char** plNames, int plSize);
void readLinesFromFile(FILE* fPtr, int* plGoals, int* plAssists, char** plNames, int numLines);
void bonusSortPlayersByBoth(int* plGoals, int* plAssists, char** plNames, int plSize);
//Main
int main(int argc, char** argv)
{
//Create new instance
FILE* fPtrIn;
FILE* fPtrOut;
//Check condition
if(argc != 3)
{
//Display
printf("Two and only two command line arguments are allowed, terminating. ");
//Exit
return 1;
}
//Open file given as command line arguments
fPtrIn = fopen(argv[1], "r");
fPtrOut = fopen(argv[2], "w");
//Check condition
if(fPtrIn == NULL || fPtrOut == NULL)
{
//Display
printf("Could not find both files, terminating. ");
//Exit
return 1;
}
//Declare the needed variables
char** plNames;
int* plGoals;
int* plAssists;
int idxMVP, idxTarget;
//Function call
int plSize = countLinesInFile(fPtrIn);
//Function call
allocateMemory(&plGoals, &plAssists,& plNames, plSize);
//Function call
readLinesFromFile(fPtrIn, plGoals, plAssists, plNames, plSize);
//Display
printf(" Read the following %d players from the file.. ", plSize);
//Function call
printPlayers(plGoals, plAssists, plNames, plSize);
//Function call
bonusSortPlayersByBoth(plGoals, plAssists, plNames, plSize);
//Display
printf(" ------ Bonus ------ ");
//Function call
printPlayers(plGoals, plAssists, plNames, plSize);
//Function call
idxMVP = findMVP(plGoals, plAssists, plSize);
//Display
printf(" MVP was %s with %d points ", *(plNames+idxMVP), (*(plGoals+idxMVP)+*(plAssists+idxMVP)));
//Allocate
char* tPlayer = (char*)malloc(MAXNAMES*sizeof(char));
//Prompt the user for input
printf("Enter a player to find: ");
//Read input
scanf("%s", tPlayer);
//Function call
idxTarget = findPlayerByName(plNames, tPlayer, plSize);
//Check condition
if(idxTarget != -1)
{
//Display
printf("%s has %d goals and %d assists ", *(plNames+idxTarget), *(plGoals+idxTarget), *(plAssists+idxTarget));
}
//Otherwise
else
{
//Display
printf("Error, player not found ");
}
//Function call
writeToFile(fPtrOut, plGoals, plAssists, plNames, plSize);
//Free memory
free(tPlayer);
free(plGoals);
free(plAssists);
//Close file
fclose(fPtrIn);
fclose(fPtrOut);
//Stop
return 0;
}
//Function countLinesInFile()
int countLinesInFile(FILE* fPtr)
{
//Initialize
int looper = TRUE, totaLine = 0;
//Allocate
char* lineCountString =(char*) malloc(MAXLINES*sizeof(char));
//Loop
while(looper == TRUE)
{
//Check condition
if(fgets(lineCountString, MAXLINES, fPtr) != NULL)
{
//Increment
totaLine++;
}
//Otherwise
else
{
//Update
looper = NOTTRUE;
}
}
//Free
rewind(fPtr);
free(lineCountString);
//Return
return totaLine;
}
//Function findPlayerByName()
int findPlayerByName(char** plNames, char* plTarget, int plSize)
{
//Variable
int idx1;
//Loop
for(idx1=0; idx1 < plSize; idx1++) {
//Check condition
if(strcmp(plTarget, *(plNames+idx1))==0)
{
//Return
return idx1;
}
}
//Return
return -1;
}
//Function findMVP
int findMVP(int* plGoals, int* plAssists, int plSize)
{
//Allocate
int* totalPoints = (int*)malloc(plSize*sizeof(int));
//Declare variables
int idx1, curMVP;
int curMax = 0;
//Loop
for(idx1=0; idx1 < plSize; idx1++) {
*(totalPoints+idx1) = *(plGoals+idx1) + *(plAssists+idx1);
//Check condition
if(*(totalPoints+idx1)> curMax)
{
//Update
curMax = *(totalPoints+idx1);
curMVP = idx1;
}
}
//Free
free(totalPoints);
//Return
return curMVP;
}
//Function printPlayers()
void printPlayers(int* plGoals, int* plAssists, char** plNames, int plSize)
{
//Display
printf("Name Goals Assists ");
//Variable
int idx1;
//Loop
for(idx1=0; idx1 < plSize; idx1++) {
//Display
printf("%-14s%-8d%d ", *(plNames+idx1), *(plGoals+idx1), *(plAssists+idx1));
}
}
//Function allocateMemory()
void allocateMemory(int** plGoals, int** plAssists, char*** plNames, int plSize)
{
//Variable
int idx1;
//Allocate
*plGoals =(int*) malloc(plSize*sizeof(int));
*plAssists =(int*) malloc(plSize*sizeof(int));
*plNames = (char**)malloc(plSize*sizeof(char*));
//Loop
for(idx1=0; idx1 < plSize; idx1++) {
//Update
*(*plNames+idx1) = (char*)malloc(MAXNAMES*sizeof(char));
}
}
//Function sortPlayersByGoals()
void sortPlayersByGoals(int* plGoals, int* plAssists, char** plNames, int plSize)
{
//Variables
int idx1, idx2, currMinimum, tAGoals, tAAssists;
//Allocate
char* tName = (char*)malloc(MAXNAMES*sizeof(char));
//Loop
for(idx1=0; idx1 < plSize; idx1++) {
//Update
currMinimum = idx1;
//Loop
for(idx2=idx1+1; idx2 < plSize; idx2++) {
//Check condition
if(*(plGoals+idx2)>*(plGoals+currMinimum))
{
//Update
currMinimum = idx2;
}
}
//Check condition
if(currMinimum != idx1)
{
//Update
tAGoals = *(plGoals+idx1);
tAAssists = *(plAssists+idx1);
strcpy(tName, *(plNames+idx1));
*(plGoals+idx1) = *(plGoals+currMinimum);
*(plAssists+idx1) = *(plAssists+currMinimum);
strcpy(*(plNames+idx1), *(plNames+currMinimum));
*(plGoals+currMinimum) = tAGoals;
*(plAssists+currMinimum) = tAAssists;
strcpy(*(plNames+currMinimum), tName);
}
}
//Free memory
free(tName);
}
//Function writeToFile()
void writeToFile(FILE* fPtr, int* plGoals, int* plAssists, char** plNames, int plSize)
{
//Variable
int idx1;
//Loop
for(idx1=0; idx1 < plSize; idx1++) {
//File write
fprintf(fPtr, "%s %d %d ", *(plNames+idx1), *(plGoals+idx1), *(plAssists+idx1));
}
rewind(fPtr);
}
//Function readLinesFromFile()
void readLinesFromFile(FILE* fPtr, int* plGoals, int* plAssists, char** plNames, int numLines)
{
//Variable
int idx1;
//Allocate
char* line = (char*)malloc(MAXLINES*sizeof(char));
char* goalString = (char*)malloc(MAXNAMES*sizeof(char));
char* assistsString = (char*)malloc(MAXNAMES*sizeof(char));
//Loop
for(idx1=0; idx1 < numLines; idx1++) {
//File get
fgets(line, MAXLINES, fPtr);
//String strtok
strtok(line, " ");
//String copy
strcpy(*(plNames+idx1), line);
//Update
goalString = strtok(NULL, " ");
assistsString = strtok(NULL, " ");
*(plGoals+idx1) = atoi(goalString);
*(plAssists+idx1) = atoi(assistsString);
}
//Free
free(line);
rewind(fPtr);
}
//Function bonusSortPlayersByBoth()
void bonusSortPlayersByBoth(int* plGoals, int* plAssists, char** plNames, int plSize)
{
//Function call
sortPlayersByGoals(plGoals, plAssists, plNames, plSize);
//Display
printf(" Sorted player list.. ");
//Function call
printPlayers(plGoals, plAssists, plNames, plSize);
//Variables
int idx1, idx2, currMinimum, tAGoals, tAAssists;
//Allocate
char* tName = (char*)malloc(MAXNAMES*sizeof(char));
//Loop
for(idx1=0; idx1 < plSize; idx1++) {
//Update
currMinimum = idx1;
//Loop
for(idx2=idx1+1; idx2 < plSize; idx2++) {
//Check condition
if(*(plGoals+idx2)>=*(plGoals+currMinimum))
{
//Check condition
if(*(plAssists+idx2)>*(plAssists+currMinimum))
{
//Update
currMinimum = idx2;
}
}
}
//Check condition
if(currMinimum != idx1)
{
//Update
tAGoals = *(plGoals+idx1);
tAAssists = *(plAssists+idx1);
strcpy(tName, *(plNames+idx1));
*(plGoals+idx1) = *(plGoals+currMinimum);
*(plAssists+idx1) = *(plAssists+currMinimum);
strcpy(*(plNames+idx1), *(plNames+currMinimum));
*(plGoals+currMinimum) = tAGoals;
*(plAssists+currMinimum) = tAAssists;
strcpy(*(plNames+currMinimum), tName);
}
}
//Free
free(tName);
}
input file
Tarasenko 5 5
Perron 2 6
McDonald 2 4
Pietrangelo 2 7
Redden 2 0
Backes 1 5
Berglund 5 2
output from program
amoeba-2:Test raji$ ./a.out
Two and only two command line arguments are allowed, terminating.
amoeba-2:Test raji$ ./a.out ./Test/players.txt out.txt
Read the following 7 players from the file..
Name Goals Assists
Tarasenko 5 5
Perron 2 6
McDonald 2 4
Pietrangelo 2 7
Redden 2 0
Backes 1 5
Berglund 5 2
Sorted player list..
Name Goals Assists
Tarasenko 5 5
Berglund 5 2
McDonald 2 4
Pietrangelo 2 7
Redden 2 0
Perron 2 6
Backes 1 5
------ Bonus ------
Name Goals Assists
Tarasenko 5 5
Berglund 5 2
Pietrangelo 2 7
Perron 2 6
McDonald 2 4
Redden 2 0
Backes 1 5
MVP was Tarasenko with 10 points
Enter a player to find: Perron
Perron has 2 goals and 6 assists
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.