Assigning a value to struct variables?
Hi guys, I'm trying to assign a value to the variables in my struct using the statement:
fscanf(fp, "%s%10s%9s%s", acct.name, acct.num, acct.pin, strbal)
but when I try to access them, it looks like the variables (acct.name, acct.num, acct.pin) are all empty. Also, can someone point me to a good reference material (that is not too hard to understand, if you know what I mean) that has basic examples? Thanks.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAXAMT 999999.99
typedef struct Account{
char name[60];
char num[20];
char pin[10];
float bal;
} Account;
Account acct;
...
void loadAcct(){
char strbal[20];
FILE *fp;
fp = fopen("/acctinfo.txt", "r+");
if (fp != NULL) {
while(4 == fscanf(fp, "%s%10s%9s%s", acct.name, acct.num, acct.pin, strbal)) {
acct.bal = atof(strbal);
printf("%s\n%s\n%s\n%f\n", acct.name, acct.num, acct.pin, acct.bal);
}
} else {
printf("Account info not found.");
}
fclose(fp);
getch();
}
Re: Assigning a value to struct variables?
I note that you are reading 4 fields with fscanf, but you check that fscanf returns 3 rather than 4.
Re: Assigning a value to struct variables?
Quote:
Originally Posted by
riechan
... when I try to access them, it looks like the variables (acct.name, acct.num, acct.pin) are all empty.
What data do you read with fscanf?
What data is outputed with printf?
Re: Assigning a value to struct variables?
How is your data stored in the file?
fscanf(fp, "%s%10s%9s%s", acct.name, acct.num, acct.pin, strbal)
The first %s will read chars from the file until it finds either a space, tab, newline or eof. Input data items stored in the file must be separated by one of these 'white-space' characters.
See
http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx
http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx
http://msdn.microsoft.com/en-us/libr...=vs.71%29.aspx
Re: Assigning a value to struct variables?
Sorry! Edited. But it still doesn't save the values into the members of acct.
Re: Assigning a value to struct variables?
Quote:
Originally Posted by
riechan
Sorry! Edited. But it still doesn't save the values into the members of acct.
That relates to post #2. What is the answer to post #4?
Re: Assigning a value to struct variables?
Quote:
Originally Posted by
riechan
Sorry! Edited. But it still doesn't save the values into the members of acct.
:confused:
Didn't you read the post#3 and post#4?
Re: Assigning a value to struct variables?
Quote:
Originally Posted by
VictorN
What data do you read with fscanf?
What data is outputed with printf?
Sorry, I hadn't refreshed the page right away (my reply was "drafted", and I just hit reply right away). Anyway, this is the data:
Rie Ishida
1234567890
123456
123456.78
Quote:
Originally Posted by 2kaud
How is your data stored in the file?
fscanf(fp, "%s%10s%9s%s", acct.name, acct.num, acct.pin, strbal)
The first %s will read chars from the file until it finds either a space, tab, newline or eof. Input data items stored in the file must be separated by one of these 'white-space' characters.
I changed the code to just test out the values like so:
Code:
while(4 == fscanf(fp, "%60s%10s%9s%s", anu, ano, pin, strbal)) {
acct.bal = atof(strbal);
And the output is now like this:
Rie
Ishida
123456789
0.000000
It is as you've said, the first %s read until the first space, which is after Rie.
Re: Assigning a value to struct variables?
For the name line, consider using fgets rather than trying to read the name using fscanf.
http://msdn.microsoft.com/en-us/libr...=VS.71%29.aspx
Re: Assigning a value to struct variables?
... 0r just split this field into two: firstName and secondName.
Re: Assigning a value to struct variables?
Quote:
Originally Posted by
VictorN
... 0r just split this field into two: firstName and secondName.
I might go for the fgets() route since I can't say if the account name consists of only two words or more. But what I want to know is why the data is not saved into acct's members...
Re: Assigning a value to struct variables?
Quote:
Originally Posted by
VictorN
... 0r just split this field into two: firstName and secondName.
But what about if there are more than 2 parts to the name? What about George Alexander Louis Windsor?;)
Re: Assigning a value to struct variables?
Quote:
Originally Posted by
riechan
I might go for the fgets() route since I can't say if the account name consists of only two words or more. But what I want to know is why the data is not saved into acct's members...
It is with my test program. I suggest you make the changes for fgets and re-post the code and we'll have another look at it.
Re: Assigning a value to struct variables?
My output:
123456.70 <- strbal
Rie Ishida
1234567890
123456
123456.703125 <- acct.bal
The extra digits in the balance is back again.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAXAMT 999999.99
typedef struct Account{
char name[60];
char num[20];
char pin[10];
float bal;
} Account;
Account acct;
...
void loadAcct(){
char strbal[20];
FILE *fp;
fp = fopen("/acctinfo.txt", "r+");
if (fp != NULL) {
fgets(acct.name, 60, fp);
while(3 == fscanf(fp, "%10s%9s%s", &acct.num, &acct.pin, strbal)) {
acct.bal = atof(strbal);
printf("%s\n", strbal);
printf("%s\n%s\n%s\n%f\n", acct.name, acct.num, acct.pin, acct.bal);
}
} else {
printf("Account info not found.");
}
fclose(fp);
getch();
}
Re: Assigning a value to struct variables?
Try this
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXAMT 999999.99
typedef struct Account{
char name[60];
char num[20];
char pin[10];
float bal;
} Account;
Account acct;
void loadAcct(){
FILE *fp;
if (fp = fopen("acctinfo.txt", "r+")) {
while (!feof(fp)) {
if (fgets(acct.name, 60, fp) && fscanf(fp, "%19s\n%9s\n%f\n", acct.num, acct.pin, &acct.bal) == 3) {
if (char *nl = strrchr(acct.name, '\n')) *nl = 0;
printf("%s\n%s\n%s\n%.2f\n", acct.name, acct.num, acct.pin, acct.bal);
}
}
} else {
printf("Account info not found.");
}
fclose(fp);
//getch();
}
int main()
{
loadAcct();
return 0;
}
With the data
Code:
Rie Ishida
1234567890
123456
123456.78
code guru
1234567890
987654321
45.67
it produces the output
Code:
Rie Ishida
1234567890
123456
123456.78
code guru
1234567890
987654321
45.67
Note that fgets reads the terminating new line so this needs to be removed.