1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

User-defined structure in C

  1. May 7, 2012 #1
    Code (Text):
    typedef struct
    {
        char firstname[MAXLEN];
        char lastname[MAXLEN];
        char color[MAXLEN];
        char baseball[MAXLEN];
        char skiing[MAXLEN];
        char tree[MAXLEN];
        int zipcode;
    } student_t;
    So this is a user defined structure that is supposed to store data about a person. It stores the first name, the last name, the favorite color, baseball preferences, skiing preferences, favorite tree, and the zipcode.

    Code (Text):
    int scanstudents()
    {
        student_t scanstudent;
        FILE *afile;

        afile = fopen("FaceBlitz.txt","r");

        fscanf(afile,"%s%s%s%s%s%s%d",&scanstudent.firstname,&scanstudent.lastname,&scanstudent.color,&scanstudent.baseball,&scanstudent.skiing,&scanstudent.tree,&scanstudent.zipcode);
        printf("%s %s %s %s %s %s %d",scanstudent.firstname,scanstudent.lastname,scanstudent.color,scanstudent.baseball,scanstudent.skiing,scanstudent.tree,scanstudent.zipcode);
        fclose(afile);

        return (0);
    }
    When I scan through the file containing all of this information, I seem to do pretty well except that instead of scanning the zipcode, I just end up with a "0" being printed out. That's not even the right number of digits. My other scans seems to do just fine, but not the zipcode scan. Advice?
     
  2. jcsd
  3. May 7, 2012 #2

    rcgldr

    User Avatar
    Homework Helper

    You dont' need to use & for the arrays like scanstudent.firstname, but that shouldn't cause problems either. I tried the code and it seemed to work ok.
     
  4. May 8, 2012 #3

    Borek

    User Avatar

    Staff: Mentor

    How does the data look? As far as I can tell US zip codes can't be stored in int as they start with letters. Not to mention ZIP+4.
     
  5. May 8, 2012 #4

    I like Serena

    User Avatar
    Homework Helper

    There might be a problem with end-of-file.
    Do you have a new line after the zip code in your input file?

    When the program is scanning for more digits in the zip code, it may encounter the end-of-file indicator.
    The result is that the zip code cannot be properly scanned and will be left at its original value (which is actually an initialized value).

    You can check this with the return code of fscanf.
    It should be 7 for 7 fields successfully scanned and converted.
    In your case it will probably be 6.
     
  6. May 8, 2012 #5
    You were quite right. Scanning does return six, and there is a line break after the zipcode in the textfile. But I thought that the fscanf function just read from the file until white space is encountered -- and I think a line break fits the definition of white space well enough. So I'm not 100% sure why this should be a problem at all (or how to fix it).
     
  7. May 8, 2012 #6

    I like Serena

    User Avatar
    Homework Helper

    Indeed fscanf reads digits from file until a non-digit is encountered, which could be white space.
    This white space has to be present.
    You can check with an additional line break.
    Or did you use letters in your zip code (as Borek suggested)?
     
  8. May 8, 2012 #7

    phinds

    User Avatar
    Gold Member
    2016 Award

    You are thinking of Canadian. US are all pure numeric.
     
  9. May 8, 2012 #8
    No, there are no letters in the zipcodes -- just numbers. What do you mean by "check with an additional line break". Will I have to write something of the form
    Code (Text):
    while ((fscanf(afile,"%d",&scanstudent.zipcode) != 10) || (fscanf(afile,"%d",&scanstudent.zipcode) != 13))
    where the 10 and 13, I think, are the ascii codes for line feed and carriage return.
     
  10. May 8, 2012 #9

    Borek

    User Avatar

    Staff: Mentor

    Strange. About two weeks ago I ordered something to be delivered in US and the ZIP code looked like KS 12345-1234. Perhaps KS is not a part of the ZIP code, but that still means you should be ready for a hyphen in the input data.
     
  11. May 8, 2012 #10

    I like Serena

    User Avatar
    Homework Helper

    No.
    It's only your input file that has to have at least 1 new line after the zip code.
    You can try to add a second one to avoid the end-of-file marker completely.
    You are not supposed to do anything in your program.

    Btw, fscanf does not return a character code or anything.
    fscanf returns the number of fields successfully scanned (which you should check).
    When you let fscanf scan only the zip code, it has to return 1 or something is wrong with your input file.
     
    Last edited: May 8, 2012
  12. May 8, 2012 #11

    Mark44

    Staff: Mentor

    KS is the abbreviation for Kansas.

    The bottom line of the address probably looked something like this:

    Topeka, KS 12345-1234

    City, State abbrev, zipcode
     
  13. May 8, 2012 #12

    jtbell

    User Avatar

    Staff: Mentor

    In practice the 5+4 digit zip code is used almost exclusively on commercial mail, by companies who get them from a database of addresses. Individuals almost always use the simpler 5-digit zip code when they have to write out the address themselves, instead of using a pre-printed envelope or sticker.

    Most people can probably tell you what their 5-digit zip code is, but I suspect very few know or remember their 5+4 digit zip code.

    For an assignment such as this one, the expectation is probably for the 5-digit zip code.
     
  14. May 8, 2012 #13
    Even when I manually add carriage returns in the file, I still get "0" for output. There is definitely whitespace following the zipcode, but this hasn't given me a solution.
     
  15. May 8, 2012 #14

    I like Serena

    User Avatar
    Homework Helper

    Another possibility is that your MAXLEN is not big enough.
    If you try for instance to read a "tree" of exactly MAXLEN characters, you may overwrite the zip code with the string-termination-zero.
    This is called an access violation.

    What is your MAXLEN?
    And what are the contents of your FaceBlitz.txt?

    And just for reference, which platform do you use (Windows, Linux, or Mac)?
    Which compiler?
    And which text editor?
     
    Last edited: May 8, 2012
  16. May 8, 2012 #15
    These are the first three lines of "FaceBlitz.txt"
    My MAXLEN is currently set to 600 (should be enough, I think). I use Eclipse on a Mac. Text editing is handled by TextEdit, which is like Notepad, I believe.

    This is how I'm scanning at the moment:

    Code (Text):
    int scanstudents()
    {
        student_t studentclass[MAXSTUDENT];
        student_t scanstudent;
        FILE *afile;
        int i;

        afile = fopen("FaceBlitz.txt","r");

        for (i = 0;i <= MAXSTUDENT;i++)
        {
            if (fscanf(afile,"%s",scanstudent.firstname) != EOF)
            {
                fscanf(afile,"%s%s%s%s%s%d",scanstudent.lastname,scanstudent.color,scanstudent.baseball,scanstudent.skiing,scanstudent.tree,&scanstudent.zipcode);
                strcpy(studentclass[i].firstname,scanstudent.firstname);
                strcpy(studentclass[i].lastname,scanstudent.lastname);
                strcpy(studentclass[i].color,scanstudent.color);
                strcpy(studentclass[i].baseball,scanstudent.baseball);
                strcpy(studentclass[i].skiing,scanstudent.skiing);
                strcpy(studentclass[i].tree,scanstudent.tree);
                studentclass[i].zipcode = scanstudent.zipcode;
                printf("%s %s %s %s %s %s %05d\n",studentclass[i].firstname,studentclass[i].lastname,studentclass[i].color,studentclass[i].baseball,studentclass[i].skiing,studentclass[i].tree,studentclass[i].zipcode);
            }
        }
        fclose(afile);
        return (studentclass);
     
  17. May 8, 2012 #16

    I like Serena

    User Avatar
    Homework Helper

    You scan 6 strings followed by a number.
    But your input contains 7 strings followed by a number.
    When you try to scan the number it gets a text string which results in a failure.
     
  18. May 8, 2012 #17

    Borek

    User Avatar

    Staff: Mentor

    Scroll back and check my first post in the thread :tongue2:
     
  19. May 8, 2012 #18

    I like Serena

    User Avatar
    Homework Helper

    Yep. The zip code starts with letters and is followed by digits. :biggrin:
     
  20. May 8, 2012 #19

    Mark44

    Staff: Mentor

    The OP's IP address indicates that he/she is a student at Dartmouth, a US university. US zip codes are either 5 numeric digits or 9 numeric digits. The zip+4 probably contains a hyphen or other separator, but neither code starts with an alpha character.

    I'm fairly certain that the 5-digit zip code is what is called for in this problem.
     
  21. May 8, 2012 #20

    Borek

    User Avatar

    Staff: Mentor

    Actually I was referring to the first question I posted, not to the ZIP thing.

    Hard to tell what is the problem not knowing what one is trying to read.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: User-defined structure in C
Loading...