无法将值从一个 double 复制到另一个并且变量重置为 0

Can't copy value from one double to another and the variables reset to 0

我遇到的问题是我正在尝试比较 2 个数字。我将 hGpa 初始化为一个字符“0.0”。读取文件时,它会获取一个 gpa 值并将其与 hGpa 进行比较。如果它更高,则将其值分配给 hGpa。该计划的重点是获得最高的 GPA。我的问题是 hGpa 的值永远不会改变。我不确定我是否分配不正确,或者我是否将变量放在错误的位置并且它们不断被覆盖。另外我不熟悉 C,所以如果这是一个愚蠢的错误,我深表歉意。这是我的代码

我已经尝试将这些值设置为全局值,但它们一直重置为 0.0000。

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>


#define MAXCHAR 1000
int main(void){



  FILE *fp;                 //pointer to file
  char *filename;           //pointer to name of file
  char *fileNames[] = {"CSCI4060U_Lab02_data/1.csv", "CSCI4060U_Lab02_data/2.csv",
                       "CSCI4060U_Lab02_data/3.csv", "CSCI4060U_Lab02_data/4.csv",
                       "CSCI4060U_Lab02_data/5.csv", "CSCI4060U_Lab02_data/6.csv",
                       "CSCI4060U_Lab02_data/7.csv"};
  long fileSize;

  int s = sizeof(fileNames) /sizeof(char*); //array consists of 8 char pointers
                                            //each char consists of 8 bytes in memory
                                            //so you get 64 / 8 = 8;



  for(int i = 0                             ; i < sizeof(fileNames)/sizeof(char*); i++){

    printf("Reading from file #%d: %s\n", i+1, fileNames[i] );
    fp = fopen(fileNames[i],"r");
    if(fp == NULL){
      printf("Could not open file %s",fileNames[i]);
      return 1;
    }
    fileSize = ftell(fp);
    char line[MAXCHAR];

    char hFirst = "";
    char hLast = "";
    char hGpa = "0.0";

    while(fgets(line, MAXCHAR, fp) != NULL){
      //printf("%s\n",line);

      char* first = strtok(line, ",");
      char* last = strtok(NULL, ",");
      char* gpa = strtok(NULL,",");

      printf("current highest: %s, %s, %s\n", hFirst, hLast, hGpa);


      double numGpa = atof(gpa);
      double numHGpa = atof(hGpa);

      printf("gpa read: %f ++++++ gpa current: %f\n",numGpa, numHGpa );

      if(&numGpa > &numHGpa){

        hFirst = first;
        hLast = last;
        hGpa = gpa;
        //printf("after assignment: %s, %s, %s\n", hFirst, hLast, hGpa);
      }



    }
    fclose(fp);

  }

  return 0;
}
    fclose(fp);

我通读的示例文件如下:

Jesse,Jordan,0.65
Charles,Austin,3.23
Peter,Cole,1.57
David,Hamilton,2.73

基本上应该进入if语句2次,改变hGpa指向的值。如果我犯了任何明显的愚蠢错误,再次抱歉。

有一次我给出了编译器生成的消息:

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[时,你没有复制它们的结果=77=] 而你总是阅读 & strtok 使用 line

所以一种可能是改变

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

我用 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)