我的代码可以读取文件并提取数字,但我不知道如何找到提取的数字之和并找到平均值。我需要帮助修改我的代码,因为我对atof函数不太熟悉。
输入文本文件样本
LivingTemp1,17.8
LivingTemp2,17.9
LivingTemp1,18.1
LivingTemp2,18.2
LivingTemp1,18.5
LivingTemp2,18.6
LivingTemp1,19.0
LivingTemp2,19.0
LivingTemp1,19.5
LivingTemp2,19.6
LivingTemp1,20.0
LivingTemp2,20.1
LivingTemp1,20.6
LivingTemp2,20.6
LivingTemp1,19.8
LivingTemp2,19.8
LivingTemp1,19.4
LivingTemp2,19.5
LivingTemp1,19.0
LivingTemp2,19.1
LivingTemp1,18.5
LivingTemp2,18.6
LivingTemp1,18.0
LivingTemp2,18.1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
FILE *fp = fopen("test.txt", "r"); `opening the text file`
FILE *fp1 = fopen("test.txt", "r");
const char s[2] = ", ";
char *token;
char c;
int count = 1;
int i;
int number[24];
if(fp != NULL)
{
char line[24];
while(fgets(line, sizeof line, fp) != NULL)
{
token = strtok(line, s); // ignoring the commas
for(i=0;i<2;i++)
{
if(i==0)
{
//printf("%s\t",token);
token = strtok(NULL,s);
} else {
double num = atof(token);
printf("%.1f\n",num); // printing the extracted numbers
}
}
}
for (c = getc(fp1); c != EOF; c = getc(fp1)){
// countingthe number of lines the text file contains
if (c == '\n') // Increment count if this character is newline
count = count + 1;
}
printf("The file has %d lines\n ",count);
fclose(fp);
} else {
perror("test.txt");
}
}
你的任务非常简单。把每一行的第二个符号转换成浮点数。如果转换成功,将这个数字加到总和中,增加数据数量。最后,报告你的数据,但要注意除以零。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void) // main returns int, not void
{
char line[80]; // make the line bige enough
int count = 0; // the count starts at zero, not at one
double sum = 0.0;
const char *filename = "test.txt";
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "Could not open '%s'.\n", filename);
exit(1);
}
while (fgets(line, sizeof line, fp)) {
const char *sep = ","; // don't include the space
char *token = strtok(line, sep);
if (token == NULL) continue; // skip empty lines
token = strtok(NULL, sep);
if (token) {
double T = atof(token);
if (T) {
sum += T;
count++;
}
}
}
fclose(fp);
if (count) {
printf("Number of measurements: %d\n", count);
printf("Average temperature: %g\n", sum / count);
} else {
puts("No data!");
}
return 0;
}
函数 atof
是非常简单的,如果一个数字不能被转换,则返回0.0。这意味着你无法区分实际的数字0(很可能是摄氏温度)和转换失败。函数 strtod
为你提供更好的不可知性。
下面是一个更简单的版本。
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int living, n, count=0;
double value, sum = 0.0;
FILE *fp = fopen("test.txt", "r"); //`opening the text file`
if( !fp ) err(EXIT_FAILURE, "no input");
while( (n = fscanf(fp, "LivingTemp%d,%lf ", &living, &value)) == 2 ) {
count++;
sum += value;
}
if( n != EOF ) {
errx(EXIT_FAILURE, "failed to parse line %d", count);
}
if( !feof(fp) ) {
err(EXIT_FAILURE, "failed to read file");
}
printf( "average of %d values is %f\n", count, sum / count );
return EXIT_SUCCESS;
}
只是为了搞笑,我还解析了 "活 "的数字。 ISTM在现实世界的情况下,你需要通过标识符来计算平均值和计数。 在你想要的情况下,这个值是解析出来的。
通常我建议先读一行再扫描。 在你的情况下,你的输入是很明确的,你可以直接扫描它。 注意,这也是对空行的补偿,因为格式字符串是以空格结束的。 它的缺点是在第一次解析失败时中止,而不是报告错误并继续结束文件。
这个 扫描f(3)函数一般来说是不被重视的。 一条 scanf 语句就可以让你省去重新发明 tokenizing、跳过whitespace和单独转换每个组件。 例如注意,转换失败会显示在转换计数中(由函数返回)。