I am trying to define a global pointer variable that can then truly be set in the main function as seen below. However I am getting a segmentation fault anytime I try to use outputName
after this. I know it probably has to do with setting the pointer equal to NULL
at the beginning... any help on how I could have a global pointer that is then set in main would be very helpful! Here is the part of my code that is giving me errors:
char* outputName = NULL;
int isNumber(char number[]){
int i;
if (number[0] == '-')
i = 1;
while(number[i] != '\0'){
if (!isdigit(number[i]))
return 0;
i++;
}
return 1;
}
void catcher(int signo){
printf("The program is exiting early");
remove(outputName);
exit(1);
}
int main(int argc, char *argv[]){
if (argc != 4){
fprintf(stderr,"Incorrect number of arguments, must supply three.\n");
exit(1);
}
char* inputName = argv[1];
outputName = argv[2];
signal(SIGINT, catcher);
int result = isNumber(argv[3]);
if (result == 0){
fprintf(stderr, "Invalid maximum line length, please enter an integer\n");
exit(1);
}
int maxChars = (atoi(argv[3])) + 1;
if ((maxChars-1) < 1){
fprintf(stderr, "Invalid third maximum line length, please enter an integer greater than zero\
.\n");
exit(1);
}
FILE* inFile = fopen(inputName, "r");
if (inFile == NULL){
fprintf(stderr, "Error while opening %s.\n", inputName);
exit(1);
}
FILE* outFile = fopen(outputName, "w");
if (outFile == NULL){
fprintf(stderr, "Error while opening %s.\n", outputName);
exit(1);
}
char line[maxChars];
int done = 0;
while (!done){
char *readLine = fgets(line, maxChars, inFile);
if (readLine == NULL){
if (errno == 0){
done = 1;
} else {
fprintf(stderr, "Error when reading line from input file");
exit(1);
}
}
int len = strlen(line);
if (line[len-1] != '\n'){
line[len] = '\n';
line[len+1] = '\0';
char current = ' ';
while (current != '\n')
current = getc(inFile);
}
if (!done){
fputs(line, outFile);
if (errno != 0){
fprintf(stderr, "Error when writing line to output file");
exit(1);
}
}
}
return 0;
}