Re: Showing problem

From: Neo (timeless_illusion_at_yahoo.com)
Date: 01/03/05


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



Relevant Pages

  • Re: Showing problem
    ... >> How can i changhe this code showing how many persons i saved to the ... > better to use char array for phonenumber field too.... ... > then it will overflow and store wrong information. ... language standard) a range of values for type 'int' from ...
    (comp.lang.c)
  • istream >> (my own string class) - low-level solution?
    ... The way my Str class manages itself of course requires that the size ... of the char array to store is known when it is allocated. ... is I don't know how many characters the word I'm going to store has. ...
    (comp.lang.cpp)
  • Re: Marshalling Message??
    ... > I need to marshall all the messages in order to send them thru a socket. ... > So I have a big char array, what is the easiest way to store an 8-byte ... int main{ ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Help Needed for Marshalling Message
    ... > marshall all the messages in order to send them thru a socket. ... > char array, what is the easiest way to store an 8-byte integer across ... "the large print giveth, and the small print taketh away" ...
    (comp.lang.c)
  • Re: Disable Command Button After Click
    ... down to the second record and back up to the first record. ... MsgBox "You must complete the store and date" _ ... Dim strSQL As String ...
    (microsoft.public.access.forms)

Loading