我想要一个读取文件并返回 ascii 值之和的函数。我看过这个页面:
How to open a file and find the longest length of a line and then print it out
我的问题的答案可能就在我眼前,但我无法完全遵循代码以使其对我有用。另外,如果您能告诉我如何查找我想要的功能,而不必在这里询问,那就太好了。谢谢。
或者这是我需要构建的东西? 当前代码:
#include <stdio.h>
#include <stdlib.h>
double get_number(int num);
main ()
{
double n1,n2,n3;
double average;
printf("\nCompute the ascii average of 3 files\n");
printf("--------------------------------\n");
n1 = get_number(1);
n2 = get_number(2);
n3 = get_number(3);
average = (n1 + n2 + n3)/3;
printf("The average is %0.2f\n",average);
}
double get_number(int num) {
double value = 0;
int c;
printf("Please input entry %i: ", num);
while ((c = getchar()) != EOF) {
value = value + (c);
}
return(value);
}
最佳答案
“ascii 值之和”的含义并不完全清楚。如果你的意思是你有一个任意数据的文件并且你想做一些像所有数据的校验和(无论它碰巧是什么),那么它非常简单,比如:
int ch;
long checksum=0;
while (EOF != (ch=getc(infile)))
checksum += ch;
如果,OTOH,你的意思是你的文件中充满了已转换为 ASCII 的数字,例如:
12 100
14
你想要的是 126,然后你会做类似的事情:
int value;
long total = 0;
while (1==scanf("%d", &value))
total += value;
关于c - 我想要一个读取文件并返回 ascii 值之和的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3747791/