Re: formatting float variables to fprintf function

From: Barry Schwarz (schwarzb_at_deloz.net)
Date: 01/02/04

  • Next message: Scott Lacy Smith: "Re: Finding memory leaks through ps command"
    Date: 2 Jan 2004 06:28:23 GMT
    
    

    On 1 Jan 2004 15:48:14 -0800, hpy_awad@yahoo.com (hpy_awad@yahoo.com)
    wrote:

    >formatting float variables to fprintf has error to my writing to
    >output file called rental and I do not the reason that a rabish is
    >written to the file instead of the actual input screen values ?
    >
    >#include <stdio.h>
    >//part09_le01_file_processing_file_setup_ver_01_iti_r01_ch09.c
    >struct name {

    Learn to indent. It will save you a lot of trouble later.

    >int int___member1;
    >float float_member2;
    >char char__member3;
    >};
    >main()
    >{
    >struct name record;
    >FILE *fpointer;
    >fpointer=fopen("rental","w");
    >char another;
    >
    >do {
    >input_record(&record);

    There is no prototype in scope for input_record.

    >fprintf(fpointer,"%4d %f %c\n",record);

    Here is your problem. fprintf will not dive into your structure to
    retrieve the members. You must do it yourself with something like
         fprintf(fpointer, "%4d %f %c\n",
                     record.int___member1,
                     record.float_member2
                     record.char__member3);

    >printf (" \nADD ANOTHER RECORD ----> ( y / n ) : ");
    >scanf("\n");
    >scanf("%c",&another);
    >} while (another=='y');
    >return 0;
    >}
    >input_record(rec)
    >struct name *rec;

    Why are you still using this obsolete style for a function definition?

    >{
    >printf("\nEnter int___member1: ");
    >scanf("%4d",&(*rec).int___member1);

    The -> operator is much preferred over the convoluted dereference
    syntax you have:
         scanf("%4d",&rec->int___member1);

    >printf("\nEnter float_member2: ");
    >scanf("%f",&(*rec).float_member2);
    >printf("\nEnter char__member3: ");
    >scanf("\n");
    >scanf("%c",&(*rec).char__member3);
    >}

    <<Remove the del for email>>


  • Next message: Scott Lacy Smith: "Re: Finding memory leaks through ps command"

    Relevant Pages