На один раз даю сообщения, выданные компилятором:
pi@raspberrypi:/tmp $ gcc -pedantic -Wall c.c
c.c: In function ‘main’:
c.c:37:19: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
char hFirst = "";
^~
c.c:38:18: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
char hLast = "";
^~
c.c:39:17: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
char hGpa = "0.0";
^~~~~
c.c:48:33: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("current highest: %s, %s, %s\n", hFirst, hLast, hGpa);
^
c.c:48:37: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
printf("current highest: %s, %s, %s\n", hFirst, hLast, hGpa);
^
c.c:48:41: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘int’ [-Wformat=]
printf("current highest: %s, %s, %s\n", hFirst, hLast, hGpa);
^
c.c:52:29: warning: passing argument 1 of ‘atof’ makes pointer from integer without a cast [-Wint-conversion]
double numHGpa = atof(hGpa);
^~~~
In file included from c.c:2:0:
/usr/include/stdlib.h:105:15: note: expected ‘const char *’ but argument is of type ‘char’
extern double atof (const char *__nptr)
^~~~
c.c:58:16: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
hFirst = first;
^
c.c:59:15: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
hLast = last;
^
c.c:60:14: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
hGpa = gpa;
^
c.c:20:7: warning: unused variable ‘s’ [-Wunused-variable]
int s = sizeof(fileNames) /sizeof(char*); //array consists of 8 char pointers
^
c.c:18:8: warning: variable ‘fileSize’ set but not used [-Wunused-but-set-variable]
long fileSize;
^~~~~~~~
c.c:13:9: warning: unused variable ‘filename’ [-Wunused-variable]
char *filename; //pointer to name of file
^~~~~~~~
c.c: At top level:
c.c:73:5: warning: data definition has no type or storage class
fclose(fp);
^~~~~~
c.c:73:5: warning: type defaults to ‘int’ in declaration of ‘fclose’ [-Wimplicit-int]
c.c:73:5: warning: parameter names (without types) in function declaration
pi@raspberrypi:/tmp $
, поэтому
char hFirst = "";
char hLast = "";
char hGpa = "0.0";
может быть
char * hFirst = "";
char * hLast = "";
char * hGpa = "0.0";
long fileSize;
и fileSize = ftell(fp)
может быть удалено
int s = sizeof(fileNames) /sizeof(char*);
может быть удалено
char *filename;
может быть удалено
окончательное fclose(fp);
должно быть удалено
Каквы видите, просто запрос предупреждений и их просмотр позволяет удалить много ошибок в вашей программе
Конечно, компилятор не может проверить семантическую
if(&numGpa > &numHGpa){
, вероятноне то, что вы хотите, может быть if(numGpa > numHGpa){
Если я поместил
Jesse,Jordan,0.65
Charles,Austin,3.23
Peter,Cole,1.57
David,Hamilton,2.73
в один файл, и я изменил fileNames , чтобы получить только его, выполнение:
pi@raspberrypi:/tmp $ ./a.out
Reading from file #1: in.csv
current highest: , , 0.0
gpa read: 0.650000 ++++++ gpa current: 0.000000
current highest: Charles, s, n
gpa read: 3.230000 ++++++ gpa current: 0.000000
current highest: Peter, le,
gpa read: 1.570000 ++++++ gpa current: 0.000000
current highest: David, Hamilton, ton
gpa read: 2.730000 ++++++ gpa current: 0.000000
имена не правильные, это потому, что вы не дублируете результат strtok при сохранении их в hFirst, hLast и hGpa в то время как вы всегда читаете & strtok , используя строку
Так что одна возможность состоит в том, чтобы изменить
char * hFirst = "";
char * hLast = "";
char * hGpa = "0.0";
...
hFirst = first;
hLast = last;
hGpa = gpa;
на:
char * hFirst = strdup("");;
char * hLast = strdup("");
char * hGpa = strdup("0.0");
...
free(hFirst);
free(hLast);
free(hGpa);
hFirst = strdup(first);
hLast = strdup(last);
hGpa = strdup(gpa);
I initiизмените значение с strdup ("") и т. д. из-за printf("current highest: %s, %s, %s\n", hFirst, hLast, hGpa);
Чтобы избежать утечек памяти, измените
fclose(fp);
на
fclose(fp);
free(hFirst);
free(hLast);
free(hGpa);
и выполнение теперь:
pi@raspberrypi:/tmp $ ./a.out
Reading from file #1: in.csv
current highest: , , 0.0
gpa read: 0.650000 ++++++ gpa current: 0.000000
current highest: Jesse, Jordan, 0.65
gpa read: 3.230000 ++++++ gpa current: 0.650000
current highest: Charles, Austin, 3.23
gpa read: 1.570000 ++++++ gpa current: 3.230000
current highest: Charles, Austin, 3.23
gpa read: 2.730000 ++++++ gpa current: 3.230000
Под valgrind :
pi@raspberrypi:/tmp $ valgrind ./a.out
==5547== Memcheck, a memory error detector
==5547== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5547== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==5547== Command: ./a.out
==5547==
Reading from file #1: in.csv
current highest: , , 0.0
gpa read: 0.650000 ++++++ gpa current: 0.000000
current highest: Jesse, Jordan, 0.65
gpa read: 3.230000 ++++++ gpa current: 0.650000
current highest: Charles, Austin, 3.23
gpa read: 1.570000 ++++++ gpa current: 3.230000
current highest: Charles, Austin, 3.23
gpa read: 2.730000 ++++++ gpa current: 3.230000
==5547==
==5547== HEAP SUMMARY:
==5547== in use at exit: 0 bytes in 0 blocks
==5547== total heap usage: 12 allocs, 12 frees, 5,518 bytes allocated
==5547==
==5547== All heap blocks were freed -- no leaks are possible
==5547==
==5547== For counts of detected and suppressed errors, rerun with: -v
==5547== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)