-
July 29th, 2013, 09:30 PM
#1
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();
}
Last edited by riechan; July 30th, 2013 at 03:22 AM.
-
July 29th, 2013, 10:47 PM
#2
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.
-
July 30th, 2013, 04:47 AM
#3
Re: Assigning a value to struct variables?
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?
Victor Nijegorodov
-
July 30th, 2013, 04:51 AM
#4
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
Last edited by 2kaud; July 30th, 2013 at 05:10 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 30th, 2013, 04:57 AM
#5
Re: Assigning a value to struct variables?
Sorry! Edited. But it still doesn't save the values into the members of acct.
-
July 30th, 2013, 05:02 AM
#6
Re: Assigning a value to struct variables?
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?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 30th, 2013, 05:03 AM
#7
Re: Assigning a value to struct variables?
Originally Posted by riechan
Sorry! Edited. But it still doesn't save the values into the members of acct.
Didn't you read the post#3 and post#4?
Victor Nijegorodov
-
July 30th, 2013, 05:29 AM
#8
Re: Assigning a value to struct variables?
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
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.
-
July 30th, 2013, 06:13 AM
#9
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
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 30th, 2013, 06:53 AM
#10
Re: Assigning a value to struct variables?
... 0r just split this field into two: firstName and secondName.
Victor Nijegorodov
-
July 30th, 2013, 07:02 AM
#11
Re: Assigning a value to struct variables?
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...
-
July 30th, 2013, 07:06 AM
#12
Re: Assigning a value to struct variables?
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?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 30th, 2013, 07:10 AM
#13
Re: Assigning a value to struct variables?
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 30th, 2013, 08:04 AM
#14
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();
}
-
July 30th, 2013, 08:23 AM
#15
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|