使用while循环文件读取问题

时间:2016-02-25 10:43:24

标签: c pointers file-io

我的readfile方法出了问题。 我无法在文本文件中打印出数字值,但我的名字很好。

这就是我的文本文件中所有内容:

Bob

10

12.00

代码:

#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;

}

这也是我的结构:

struct account
{
  char name[25];
  int accountno;
  float balance;
};

1 个答案:

答案 0 :(得分:6)

&您不需要printf

printf("name = %s number = %d balance = %f\n", name, &accountNum, &money); 

使用以下打印声明(通知'&'已删除。仅用于读取scanf中的值)

printf("name = %s number = %d balance = %f\n", name, accountNum, money);