# User-defined structure in C

Code:
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:
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?

Related Engineering and Comp Sci Homework Help News on Phys.org
rcgldr
Homework Helper
Code:
int scanstudents()
{ ... &scanstudent.firstname ... }
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.

Borek
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.

I like Serena
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.

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).

I like Serena
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)?

phinds
Gold Member
2019 Award
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.
You are thinking of Canadian. US are all pure numeric.

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:
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.

Borek
Mentor
You are thinking of Canadian. US are all pure numeric.
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.

I like Serena
Homework Helper
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:
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.
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:
Mark44
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.
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

jtbell
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.

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.
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.

I like Serena
Homework Helper
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.
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.

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:
These are the first three lines of "FaceBlitz.txt"
Roger Black blue Paneer_chili right-handed Ski_Jumping Mango 03755
Henry Abaum green Chocolate_cake right-handed Downhill Linden 84121
Michael Travis blue Fabulous_hamburger right-handed Downhill Sugar_maple 55391
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:
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);

I like Serena
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.

Borek
Mentor
Scroll back and check my first post in the thread :tongue2:

I like Serena
Homework Helper
Scroll back and check my first post in the thread
Yep. The zip code starts with letters and is followed by digits.

Mark44
Mentor
Scroll back and check my first post in the thread :tongue2:
Yep. The zip code starts with letters and is followed by digits.
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.

Borek
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.

I like Serena
Homework Helper
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.
The zip code in the OP's problem does not start with letters, but his program is trying to interpret the text string that comes before the zip code as a numeric zip code.