Assigning a value to struct variables?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 29

Thread: Assigning a value to struct variables?

  1. #1
    Join Date
    Jul 2013
    Posts
    30

    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 04:22 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,355

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,533

    Re: Assigning a value to struct variables?

    Quote Originally Posted by riechan View Post
    ... 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

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,746

    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 06:10 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Jul 2013
    Posts
    30

    Re: Assigning a value to struct variables?

    Sorry! Edited. But it still doesn't save the values into the members of acct.

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,746

    Re: Assigning a value to struct variables?

    Quote Originally Posted by riechan View Post
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,533

    Re: Assigning a value to struct variables?

    Quote Originally Posted by riechan View Post
    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

  8. #8
    Join Date
    Jul 2013
    Posts
    30

    Re: Assigning a value to struct variables?

    Quote Originally Posted by VictorN View Post
    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.

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,746

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,533

    Re: Assigning a value to struct variables?

    ... 0r just split this field into two: firstName and secondName.
    Victor Nijegorodov

  11. #11
    Join Date
    Jul 2013
    Posts
    30

    Re: Assigning a value to struct variables?

    Quote Originally Posted by VictorN View Post
    ... 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...

  12. #12
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,746

    Re: Assigning a value to struct variables?

    Quote Originally Posted by VictorN View Post
    ... 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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  13. #13
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,746

    Re: Assigning a value to struct variables?

    Quote Originally Posted by riechan View Post
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #14
    Join Date
    Jul 2013
    Posts
    30

    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();
    }

  15. #15
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,746

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center