不能从一个双值复制到另一个变量重置为0

问题描述 投票:0回答:2

我的问题是,我想比较2个数字。我初始化hGpa作为char“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指向。再次抱歉,如果我做任何愚蠢的明显错误。

c file-io double fgets
2个回答
0
投票

对于一次我给由编译器产生的信息:

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

在一个文件中,我更改文件名只拥有它,执行的是:

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

我初始化因为printf("current highest: %s, %s, %s\n", hFirst, hLast, hGpa);的的strdup(“”)等

为了避免内存泄漏变化

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)
© www.soinside.com 2019 - 2024. All rights reserved.