gpt4 book ai didi

c - 如何用 C 语言从文件中读取文本/数字

转载 作者:行者123 更新时间:2023-11-30 14:56:40 25 4
gpt4 key购买 nike

我有这段代码来编写一个名为Phone Bill.txt的文件

#include<stdio.h>
int main(){
FILE *file;
file=fopen("Phone Bill.txt","w");

if(file!=NULL){
fprintf(file, "Number\t\tLocal Call Chargers\tInternational Call Charges\tRoaming Charges\n");
}

double phoneNumber;
float localCharges, internationalCharges, roamingCharges;
char wantToContinue='Y';

while(wantToContinue=='Y'){
printf("Enter Phone Number: ");
scanf("%lf",&phoneNumber);
printf("Enter Local Call Charges: ");
scanf("%f",&localCharges);
printf("Enter International Call Charges: ");
scanf("%f",&internationalCharges);
printf("Enter Roaming Call Charges: ");
scanf("%f",&roamingCharges);

if (file!=NULL)
fprintf(file, "%.0lf\t%.0f\t\t\t%.0f\t\t\t\t%.0f\n",phoneNumber,localCharges,internationalCharges,roamingCharges);

printf("\n");
printf("Want to Continue Writing? (Y/N)");
scanf(" %c",&wantToContinue);
if(wantToContinue=='Y'){
wantToContinue='Y';
}else if(wantToContinue=='N'){
wantToContinue='N';
fclose(file);
}
printf("\n");
}



return 0;
}

以下是将内容写入文件后的 Phone Bill.txt 的屏幕截图:

Phone Bill Screenshot

这是我用来从 Phone Bill.txt 文件中读取内容(数字)的代码:

#include<stdio.h>
int main(){
FILE *file;

char strings[200];
double phoneNumber;
float localCharges, internationalCharges, roamingCharges;

file=fopen("Phone Bill.txt","r");

if(file!=NULL){
fscanf(file,"%.0lf\t%.0f\t\t\t%.0f\t\t\t\t%.0f\n",phoneNumber,localCharges,internationalCharges,roamingCharges);
printf("%.0lf %0.f %0.f %0.f",phoneNumber,localCharges,internationalCharges,roamingCharges);
}
return 0;
}

但我的输出是:

0 0 0 0

您能告诉我为什么会收到此错误吗???*

谢谢!

最佳答案

首先,您尝试扫描文件的第一行,即标题

其次,您需要消除 fscanf 格式定义中的所有干扰:这些格式类型会自动忽略前导空格。

第三,您需要将地址传递给fscanf。这是一个工作编辑。

#include <stdio.h>

int main(){
FILE *file;
char strings[200];
double phoneNumber;
float localCharges, internationalCharges, roamingCharges;
file=fopen("Phone Bill.txt","r");
if(file!=NULL){
fgets(strings, sizeof strings, file); // read the headings
fscanf(file,"%lf%f%f%f", &phoneNumber, &localCharges, &internationalCharges, &roamingCharges);
printf("%.0lf %0.f %0.f %0.f", phoneNumber, localCharges, internationalCharges, roamingCharges);
fclose(file); // don't forget to close
}
return 0;
}

此外,您必须检查 fscanf 的返回值,该值应该是扫描的项目数 4

最后,我建议 phoneNumber 不应该是 double 甚至 unsigned long long,而应该是 char phoneNumber[MAXPHLEN ]。电话号码可以以 000 开头,否则将会丢失。更一般地说,这也将适应 IDD 中使用的 + 以及某些电话拨号盘上曾经使用且可能仍然使用的数字字母等效项。

编辑来自OP关于使用字符串作为电话号码的评论。我限制字符串输入长度以防止溢出,并检查扫描的项目数 - 在循环中读取所有数据。

#include <stdio.h>

#define MAXPHLEN 50

int main(){
FILE *file;
char strings[200];
char phoneNumber[MAXPHLEN];
float localCharges, internationalCharges, roamingCharges;
file=fopen("Phone Bill.txt","r");
if(file!=NULL){
fgets(strings, sizeof strings, file); // read the headings
while((fscanf(file,"%49s%f%f%f", phoneNumber, &localCharges, &internationalCharges, &roamingCharges)) == 4) {
printf("%s %0.f %0.f %0.f\n", phoneNumber, localCharges, internationalCharges, roamingCharges);
}
fclose(file); // don't forget to close
}
return 0;
}

关于c - 如何用 C 语言从文件中读取文本/数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44486795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com