【发布时间】:2014-05-02 06:55:53
【问题描述】:
所以我得到了我的程序的整个设置,我能够读取整个文件内容。我唯一需要的就是能够添加每一列并将其放入我制作的变量中。第一列是英里,第二列是加仑。那么我怎样才能使用我的代码使其成为可能。
54 250 19
62 525 38
71 123 6
85 1322 86
97 235 14
#include <stdio.h>
#include <conio.h>
int main()
{
// pointer file
FILE *pFile;
char line[128];
int miles;
int gallons;
int mpg;
// opening name of file with mode
pFile = fopen("Carpgm.txt","r");
//headers
printf("Car No. Miles Driven Gallons Used\n");
//checking if file is real and got right path
if (pFile != NULL)
{
while (fgets(line, sizeof(line), pFile) != NULL)
{
int a, b, c;
if (sscanf(line, "%d %d %d", &a, &b, &c) == 3)
{
/* Values read */
printf("%d %d %d\n",a, b, c);
}
}
mpg = miles / gallons;
printf("Total miles driven: \n",miles);
printf("Total Gallons of gas: \n",gallons);
printf("Average MPG: \n",mpg);
printf("%d",a);
//closing file
fclose(pFile);
}
else
{
printf("Could not open file\n");
}
getch();
return 0;
}
【问题讨论】:
-
很抱歉,我看不到您的问题。你想把哪些值加起来放在哪里?顺便说一句,你只提到了两列,但我看到了三列,所以也许你忘了一列(车号?)。
-
我想将第二列编号和第三列编号相加,但我无法单独获得每个编号。
-
换句话说,我怎样才能单独抓住每个数字
-
所以如果我想将 250 + 525 添加到 235,我该怎么做
-
那么你想把第二列和第三列相加吗?