[英]How to read Texts/Numbers from a File in C
我有這段代碼來編寫一個名為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.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]
。 電話號碼可以以0
或00
開頭,否則將丟失。 更一般而言,這也將適應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;
}
更改fscanf以指向變量:
所以:
fscanf(file,"%.0lf\t%.0f\t\t\t%.0f\t\t\t\t%.0f\n",&phoneNumber,&localCharges,&internationalCharges,&roamingCharges)
通常,應始終使代碼盡可能簡單(並且不要更簡單)
建議的代碼如下:
注意:由於文件已經格式化,因此只需要在終端上回顯這些行即可。
#include <stdio.h> // fgets(), fopen(), fclose(), puts(), perror()
#include <stdlib.h> // exit(), EXIT_FAILURE
int main( void )
{
char strings[200];
FILE *fp = fopen("Phone Bill.txt","r");
if( !fp )
{
perror( "fopen to read Phone Bill.txt failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
// read, print each line:
while( fgets( strings, sizeof( strings ), fp ) )
{
puts( strings );
}
if( !feof( fp ) )
{ // then read error
perror( "read of Phone Bill.txt failed" );
fclose( fp );
exit( EXIT_FAILURE );
}
fclose( fp );
return 0;
} // end function: main
關於OP發布代碼以讀取文件的信息:
\\t
是“空白”,因此不應采用fscanf()
格式的字符串
文件的第一行不是數據的一部分,因此需要單獨讀取,建議使用fgets()
讀入strings[]
然后可以立即使用puts()
可以通過以下內容輕松讀取文件末尾的第二行:
while( 4 == fscanf( file, " %lf %f %f %f", &phoneNumber, &localCharges, &internationalCharges, &roamingCharges ) )
{
....
}
fscanf()
的格式字符串可以進一步減少(因為%f和%lf輸入/格式規范將前導“空白”丟棄為:
"%lf%f%f%f"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.