formatting float variables to fprintf function
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > formatting float variables to fprintf function




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    formatting float variables to fprintf function  
hpy_awad@yahoo.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:21 PM

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_v
er_01_iti_r01_ch09.c
struct name {
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);
fprintf(fpointer,"%4d %f %c\n",record);
printf (" \nADD ANOTHER RECORD ----> ( y / n )  :  ");
scanf("\n");
scanf("%c",&another);
} while (another=='y');
return 0;
}
input_record(rec)
struct name *rec;
{
printf("\nEnter int___member1: ");
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);
}





[ Post a follow-up to this message ]



    Re: formatting float variables to fprintf function  
Alan Coopersmith


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:21 PM

hpy_awad@yahoo.com (hpy_awad@yahoo.com) writes in comp.unix.solaris:
|struct name {
|int   int___member1;
|float float_member2;
|char  char__member3;
|};
|fprintf(fpointer,"%4d %f %c\n",record);

You only provide one argument to fprintf, but ask it to print 3 things,
so it just makes up random garbage for the values of the second and
third.  You must specify a value for every %-specifier - you can't just
put the struct there - you need to do:
fprintf(fpointer,"%4d %f %c\n", record.int___member1,
record.float_member2, record.char__member3);

--
 ________________________________________
________________________________
Alan Coopersmith                              alanc@alum.calberkeley.org
http://www.CSUA.Berkeley.EDU/~alanc/       aka: Alan.Coopersmith@Sun.COM
Working for, but definitely not speaking for, Sun Microsystems, Inc.





[ Post a follow-up to this message ]



    Re: formatting float variables to fprintf function  
Barry Schwarz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10:21 PM

On 1 Jan 2004 15:48:14 -0800, hpy_awad@yahoo.com (hpy_awad@yahoo.com)
wrote:
quote:
>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_v er_01_iti_r01_ch09.c >struct name {
Learn to indent. It will save you a lot of trouble later.
quote:
>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.
quote:
>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);
quote:
>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?
quote:
>{ >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);
quote:
>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>>




[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:22 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register