Re: Showing problem
From: Neo (timeless_illusion_at_yahoo.com)
Date: 01/03/05
- Next message: Neo: "Re: which loop is faster"
- Previous message: EventHelix.com: "Re: which loop is faster"
- In reply to: beginner10: "Showing problem"
- Next in thread: beginner10: "Re: Showing problem"
- Reply: beginner10: "Re: Showing problem"
- Reply: beginner10: "Re: Showing problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 3 Jan 2005 17:53:56 +0530
"beginner10" <petri_leskinen@yahoo.com> wrote in message
news:d9e0472019dc0fd37decf5be82d50d86@localhost.talkaboutprogramming.com...
>
> How can i changhe this code showing how many persons i saved to the file?
> And it prints pretty much rubbish. Where is the problem?
>
> #include <stdio.h>
> int main()
> {
> int i;
> FILE *data_file;
> char list[] = "list.txt";
>
> struct person {
> char firstname[20];
> char lastname[25];
> int phonenumber;
better to use char array for phonenumber field too....
lets say user input something like : 9899325025
then it will overflow and store wrong information.
by using char array you have more precise control
over how many digits you want to store in phonenumber
field.
>
> };
>
> struct person person_list[50];
Oops! use #defines instead of magic numbers....
like #define MAX_RECORDS 50
>
> for(i=0; i<1; i++) {
>
> printf("Give your firstname:");
> scanf("%s", person_list[i].firstname);
>
> printf("Give your lastname:");
> scanf("%s", person_list[i].lastname);
>
> printf("Give your phone number:");
> scanf("%d", &person_list[i].phonenumber);
>
> }
>
> if ((data_file = fopen(list, "w")) == NULL) {
> printf("Error opening file");
> return 0;
> } else {
>
> for(i=0; i<=2; i++)
> {
you have valid data only in first record, but here you are accessing
and storing second and third row also... that contains garbage.
either properly initialize the whole array in the begining of the program
if you want to store more records then input from the user.
or better keep this loop counter same as above.
Hope this will help U....
-Neo
> fprintf(data_file, "%s %s %d\n", person_list[i].firstname,
> person_list[i].lastname, person_list[i].phonenumber);
> }
> }
> fclose(data_file);
> printf("Data saved succesfully!");
> return 0;
> }
>
>
- Next message: Neo: "Re: which loop is faster"
- Previous message: EventHelix.com: "Re: which loop is faster"
- In reply to: beginner10: "Showing problem"
- Next in thread: beginner10: "Re: Showing problem"
- Reply: beginner10: "Re: Showing problem"
- Reply: beginner10: "Re: Showing problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|