C 程序,从一个文件中获取一对数字,计算 newton(n,k),并将答案写入另一个文件
C Program that takes pairs of numbers from a file, calculates newton(n,k), and writes answer to another file
我需要一个程序的帮助,该程序从 txt 文件中获取一对数字,计算牛顿系数 (n!/(n!.(n-k)!)) , 并将答案(分数)写入另一个 txt 文件。现在我有这个:
#include <stdio.h>
void factorial() {
long l1, l2;
long score = 1;
for (int i = 1; i < l2; i++) {
score = (score * (l1 - i + 1) / i);
}
}
void read() {
long l1, l2;
long score = 1;
FILE *file = fopen("pairs.txt", "r");
FILE *file2 = fopen("sum.txt", "r");
while (fscanf(file, "%ld%ld", &l1, &l2) == 2) {
factorial();
fprintf(file2, "%ld", score);
}
printf("Score is: %ld", score);
fclose(file);
fclose(file2);
}
int main() {
read();
return 1;
}
问题是,当我启动程序时,它显示答案 Score is: 1
,而文件 sum.txt.[=12 中没有任何内容=]
您的代码中存在多个问题:
您必须将参数传递给 binomial
函数,return 使用 return
语句的结果并将 return 值存储在调用中代码.
您计算牛顿二项式系数的函数不正确。
您应该打开输出文件 sum.txt 以使用 "w"
模式字符串写入。
您应该检查 fopen()
是否成功打开文件。正如发布的那样,您的代码可能无法打开不存在的输出文件 sum.txt,因为它试图打开它进行阅读。因此 file2
是 NULL
并且使用空流指针调用 fprintf
具有未定义的行为。这可以解释您观察到的崩溃。
这是更正后的版本:
#include <errno.h>
#include <stdio.h>
#include <string.h>
long binomial(long n, long k) {
long value = 1;
if (k < n - k) {
k = n - k;
}
for (long i = n; i > k; i--) {
value *= i;
}
for (long i = k; i > 1; i++) {
value /= i;
}
return value;
}
int read(void) {
long n, k, score;
FILE *file1, *file2;
file = fopen("pairs.txt", "r");
if (file == NULL) {
fprintf(stderr, "error opening pairs.txt: %s\n", strerror(errno));
return 1;
}
file2 = fopen("sum.txt", "w");
if (file2 == NULL) {
fprintf(stderr, "error opening sum.txt: %s\n", strerror(errno));
fclose(file);
return 1;
}
while (fscanf(file, "%ld%ld", &n, &k) == 2) {
score = binomial(n, k);
fprintf(file2, "%ld\n", score);
}
//printf("Score is: %ld\n", score);
fclose(file);
fclose(file2);
return 0;
}
int main(void) {
return read();
}
当您希望将输出存储在另一个文件中时,为什么要打开一个文件并在终端中使用 printf 打印结果?如果你想把一个程序的输出放在一个文件中,重定向是使用“<”运算符(对于标准输入)完成的。
当 运行 您的程序(您的 fscanf 行)时也存在分段错误。
你能告诉我们你的输入文件的一个版本吗(pairs.txt)?
我需要一个程序的帮助,该程序从 txt 文件中获取一对数字,计算牛顿系数 (n!/(n!.(n-k)!)) , 并将答案(分数)写入另一个 txt 文件。现在我有这个:
#include <stdio.h>
void factorial() {
long l1, l2;
long score = 1;
for (int i = 1; i < l2; i++) {
score = (score * (l1 - i + 1) / i);
}
}
void read() {
long l1, l2;
long score = 1;
FILE *file = fopen("pairs.txt", "r");
FILE *file2 = fopen("sum.txt", "r");
while (fscanf(file, "%ld%ld", &l1, &l2) == 2) {
factorial();
fprintf(file2, "%ld", score);
}
printf("Score is: %ld", score);
fclose(file);
fclose(file2);
}
int main() {
read();
return 1;
}
问题是,当我启动程序时,它显示答案 Score is: 1
,而文件 sum.txt.[=12 中没有任何内容=]
您的代码中存在多个问题:
您必须将参数传递给
binomial
函数,return 使用return
语句的结果并将 return 值存储在调用中代码.您计算牛顿二项式系数的函数不正确。
您应该打开输出文件 sum.txt 以使用
"w"
模式字符串写入。您应该检查
fopen()
是否成功打开文件。正如发布的那样,您的代码可能无法打开不存在的输出文件 sum.txt,因为它试图打开它进行阅读。因此file2
是NULL
并且使用空流指针调用fprintf
具有未定义的行为。这可以解释您观察到的崩溃。
这是更正后的版本:
#include <errno.h>
#include <stdio.h>
#include <string.h>
long binomial(long n, long k) {
long value = 1;
if (k < n - k) {
k = n - k;
}
for (long i = n; i > k; i--) {
value *= i;
}
for (long i = k; i > 1; i++) {
value /= i;
}
return value;
}
int read(void) {
long n, k, score;
FILE *file1, *file2;
file = fopen("pairs.txt", "r");
if (file == NULL) {
fprintf(stderr, "error opening pairs.txt: %s\n", strerror(errno));
return 1;
}
file2 = fopen("sum.txt", "w");
if (file2 == NULL) {
fprintf(stderr, "error opening sum.txt: %s\n", strerror(errno));
fclose(file);
return 1;
}
while (fscanf(file, "%ld%ld", &n, &k) == 2) {
score = binomial(n, k);
fprintf(file2, "%ld\n", score);
}
//printf("Score is: %ld\n", score);
fclose(file);
fclose(file2);
return 0;
}
int main(void) {
return read();
}
当您希望将输出存储在另一个文件中时,为什么要打开一个文件并在终端中使用 printf 打印结果?如果你想把一个程序的输出放在一个文件中,重定向是使用“<”运算符(对于标准输入)完成的。
当 运行 您的程序(您的 fscanf 行)时也存在分段错误。 你能告诉我们你的输入文件的一个版本吗(pairs.txt)?