0

I'm having trouble with my readfile method. 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;
};
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Zeroguy
  • 1
  • 1
  • 1
  • 1
    `fscanf()` *can* return -1; (and `while( !feof()) {}` is always wrong) – joop Feb 25 '16 at 10:46
  • Also you sure the name never contains more than 24 characters? – Neijwiert Feb 25 '16 at 10:50
  • 1
    If `fscanf()` returns 0, your code seems likely to go into an infinite loop. And `http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong` is wrong. See http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Andrew Henle Feb 25 '16 at 10:51
  • 1
    And `fopen` NULL return value does not mean that the file is empty. – Neijwiert Feb 25 '16 at 10:52
  • Is that why I get an infinite loop? I thought by using while(! foef()) the function would stop reading at the end of the file. – Zeroguy Feb 25 '16 at 11:09
  • 1
    @CoolGuy Here: If fscanf() returns 0, your code seems likely to go into an infinite loop. And `while(!feof(inputFile))` is wrong. See http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Andrew Henle Feb 25 '16 at 11:24

1 Answers1

6

You don't need & for 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)

printf("name = %s number = %d balance = %f\n", name, accountNum, money); 
Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
  • This would probably only solve half the problems. Since the values are delimited by new line separators and probably preceded by a carriage return. – Neijwiert Feb 25 '16 at 10:58
  • @Neijwiert I don't think so. Doesn't the `fopen` treat `\r\n` as `\n` because the second argument of `fopen` is `"r"`? – Spikatrix Feb 25 '16 at 11:01
  • Thank you. With "&" I was printing the address of my variables instead of the values. – Zeroguy Feb 25 '16 at 11:05
  • @CoolGuy I apologize, guess my C is a bit rusty. "A single whitespace in the format string validates any quantity of whitespace characters extracted from the stream (including none)." – Neijwiert Feb 25 '16 at 11:07
  • @Zeroguy Remove the outer loop and change `while(fscanf(inputFile,"%s %d %f", name,&accountNum,&money))` to `while(fscanf(inputFile,"%s %d %f", name,&accountNum,&money) == 3)` – Spikatrix Feb 25 '16 at 11:07
  • @CoolGuy Thank you, I changed it and it worked! Is the "=3" suppose to help stop the loop? Since it's only reading three things? – Zeroguy Feb 25 '16 at 11:16
  • @Zeroguy It is to make sure `fscanf` succeeded in reading 3 things, specifically, a word, a number and a floating-point nunber. If you look at the [documentation of `fscanf`](http://pubs.opengroup.org/onlinepubs/009695399/functions/fscanf.html), you'll see that it returns the number of items successfully scanned and assigned – Spikatrix Feb 25 '16 at 11:19
  • @Zeroguy Oh. And FYI, use `%p` to print addresses, not `%d`. – Spikatrix Feb 25 '16 at 11:22
  • What if instead of "Bob" for name, I wanted "Bob Builder" as a name? How could I get both? – Zeroguy Feb 25 '16 at 11:41
  • Try using function getline – Digital_Reality Feb 25 '16 at 12:05