I'm relatively new to C and just finished a train station schedule program that looks for the nearest time departing time, from a schedule file, relative to the user input time, source, and destination. The restrictions were no string.h and no dynamic memory allocation of any kind. The program worked fine when I tested it but when marked, it produced AddressSanitizer errors which we haven't learned yet. I'm not sure how to read the error code. The Following is my code:
#include <stdio.h>
#include <stdlib.h>
#define lineSize 4096
int main(int argc, char* argv[]) {
if(argc != 4){
printf("Please provide <source> <destination> <time> as command line arguments\n");
return(0);
}
int c = 0;
while(argv[3][c] != 0){
c++;
}
if(c < 8){
printf("Please enter a proper time\n");
return(0);
}
char line[lineSize];
FILE * fpointer = fopen("timetable.list", "r");
int i;
int j;
int StartSize;
int DestinationSize;
int DepartTime;
for(StartSize = 0; argv[1][StartSize] != '\0'; StartSize += 1){
continue;
}
for(DestinationSize = 0; argv[2][DestinationSize] != '\0'; DestinationSize += 1){
continue;
}
for(DepartTime = 0; argv[3][DepartTime] != '\0'; DepartTime += 1){
continue;
}
int hour = 23;
int min = 59;
int sec = 59;
int check = 1;
int matchStart = 0;
int matchEnd = 0;
int checkNearestTime = 0;
int isThereATime = 0;
while(fgets(line, lineSize, fpointer) != NULL){
check = 1;
for(i = 0; i < StartSize; i++){
if(argv[1][i] == line[i]){
matchStart = 1;
continue;
}
else{
check = 0;
matchStart = 0;
}
}
if(check == 1){
i = 0;
int j = StartSize + 2;
while(i < DestinationSize){
if(argv[2][i] == line[j]){
i += 1;
j += 1;
matchEnd = 1;
}
else{
check = 0;
matchEnd = 0;
break;
}
}
}
if(check == 1){
i = 0;
j = StartSize + DestinationSize + 4;
while(i < DepartTime){
if(argv[3][i] == line[j]){
i += 1;
j += 1;
}
else{
check = 0;
break;
}
}
if(check == 1){
printf("The next train to %s from %s departs at %s\n", argv[2], argv[1], argv[3]);
return 0;
}
if(check == 0){
j = StartSize + DestinationSize + 4;
int lineFullHour = (line[j] - '0') * 10 + (line[j+1] -'0');
int lineFullMin = (line[j+3] - '0') * 10 + (line[j+4] -'0');
int lineFullSec = (line[j+6] - '0') * 10 + (line[j+7] -'0');
int argvHour = (argv[3][0] - '0') * 10 + (argv[3][1]-'0');
int argvMin = (argv[3][3] - '0') * 10 + (argv[3][4] -'0');
int argvSec = (argv[3][6] - '0') * 10 + (argv[3][7] -'0');
if(lineFullHour > argvHour){
if(lineFullHour < hour){
hour = lineFullHour;
min = lineFullMin;
sec = lineFullSec;
checkNearestTime = 1;
}
else if(lineFullHour == hour){
if(lineFullMin < min){
min = lineFullMin;
sec = lineFullSec;
checkNearestTime = 1;
}
else if(lineFullMin == min){
if(lineFullSec < sec){
sec = lineFullSec;
checkNearestTime = 1;
}
}
}
}
else if(lineFullHour == argvHour && lineFullMin > argvMin){
if(lineFullHour < hour){
hour = lineFullHour;
min = lineFullMin;
sec = lineFullSec;
checkNearestTime = 1;
}
else if(lineFullHour == hour){
if(lineFullMin < min){
min = lineFullMin;
sec = lineFullSec;
checkNearestTime = 1;
}
else if(lineFullMin == min){
if(lineFullSec < sec){
sec = lineFullSec;
checkNearestTime = 1;
}
}
}
}
else if(lineFullHour == argvHour && lineFullMin == argvMin && lineFullSec > argvSec){
if(lineFullHour < hour){
hour = lineFullHour;
min = lineFullMin;
sec = lineFullSec;
checkNearestTime = 1;
}
else if(lineFullHour == hour){
if(lineFullMin < min){
min = lineFullMin;
sec = lineFullSec;
checkNearestTime = 1;
}
else if(lineFullMin == min){
if(lineFullSec < sec){
sec = lineFullSec;
checkNearestTime = 1;
}
}
}
}
}
}
if(matchStart == 1 && matchEnd == 1 && checkNearestTime == 1){
isThereATime = 1;
}
}
if(isThereATime == 1){
printf("The next train to %s from %s departs at %02d:%02d:%02d\n", argv[2], argv[1], hour, min, sec);
return 0;
}
if(isThereATime == 0){
printf("No suitable trains can be found\n");
return 0;
}
return 0;
}
The source code (line 53) and your report do not match (line 59) do not match. It complains about a NULL pointer, per @kaylum, that only happens if fpointer is NULL. In turn this means │FILE * fpointer = fopen("timetable.list", "r");
failed. Always implement error checking:
#include <errno.h>
#include <string.h>
...
FILE * fpointer = fopen("timetable.list", "r");
if(!fpointer) {
fprintf(stderr, "fopen failed with %s", strerror(errno)));
exit(1);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.