[英]File reading problems using while loop
I'm having trouble with my readfile
method. 我的
readfile
方法遇到问题。 I can't print out the number values in my text file, but my name is fine. 我无法在文本文件中打印出数字值,但是我的名字很好。
This is all that is inside my text file: 这就是我的文本文件中的全部内容:
Bob
10
12.00
Code: 码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "iofunctions.h"
int readfile( struct account accarray[], int* numcust, char filename[])
{
/* variables */
FILE *inputFile;
char line[25];
char spaceEater[25];
char name[25]={"\0"};
int accountNum;
float money ;
inputFile = fopen("People_Info.txt", "r");
if(inputFile == NULL)
{
fprintf(stderr, "Sorry, I cannot open this file. Also it's empty.\n");
exit(1);
}
/*outer loop is to check if file is not null*/
while(!feof(inputFile))
{
/*while file has something*/
while(fscanf(inputFile,"%s %d %f", name,&accountNum,&money))
{
accarray->accountno = accountNum;
accarray->balance = money;
printf("name = %s number = %d balance = %f\n", name, &accountNum, &money);
}
}
fclose(inputFile);
return 0;
}
Also this is my struct: 这也是我的结构:
struct account
{
char name[25];
int accountno;
float balance;
};
You don't need &
for printf
. 你不需要
&
对printf
。
printf("name = %s number = %d balance = %f\n", name, &accountNum, &money);
Use below print statement (notice '&'
is removed. Its only used to reading value in scanf
) 在下面的打印语句中使用(注意删除了
'&'
。它仅用于读取scanf
值)
printf("name = %s number = %d balance = %f\n", name, accountNum, money);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.