我遇到的问题是我正在尝试比较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语句并更改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
名称不正确,这是因为在将它们保存到hFirst,hLast和hGpa中的同时始终使用line读取和strtok时,您不会复制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)
关于c - 无法将值从一个 double 复制到另一个 double 并且变量重置为0,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54560302/