473,241 Members | 1,500 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

formatting float variables to fprintf function

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 {
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);
}
Nov 14 '05 #1
2 9500
hp******@yahoo.com (hp******@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 al***@alum.calberkeley.org
http://www.CSUA.Berkeley.EDU/~alanc/ aka: Al**************@Sun.COM
Working for, but definitely not speaking for, Sun Microsystems, Inc.
Nov 14 '05 #2
On 1 Jan 2004 15:48:14 -0800, hp******@yahoo.com (hp******@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>>
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Pierre Fortin | last post by:
Hi! "Python Essential Reference" - 2nd Ed, on P. 47 states that a string format can include "*" for a field width (no restrictions noted); yet... >>> "%*d" % (6,2) # works as expected ' ...
1
by: Richard | last post by:
I am new to C++ and I do not understand the formatting code very well. I need to format my output to look like this: Movie Name: "Death Grip" Adult Tickets Sold: ...
3
by: Allan Bruce | last post by:
Hi there, I am dumping a lot of floats to file and wish to reduce the filesize. This is my main loop (looped several thousand times) fprintf(fptr, "%f %f %f ", normals, normals, normals); //...
16
by: Gerald Lafreniere | last post by:
{ float F=123.456000; F*=1000; // Actually I used a for loop F*=10 three times. printf("%f\n", F); } This will produce something like 123456.00XXXX, where XXXX are garbage digits. Why...
122
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) {...
43
by: Xancatal | last post by:
Hey everybody. I need help on this one. I need to verify that a number entered by a user is not either a negative number (-100.00), or an alphabet (a, b, c, X, Y) as well as other number other than...
6
by: Rafael Olarte | last post by:
The goal of this project is to output the following information as follows: 34.5 38.6 4.1 42.4 3.8 close 46.8 4.4 big change. The values of the first colunm are obtain from a file...
111
by: Nate | last post by:
Hello, I am looking for a method to automatically declare variables in C. I'm not sure if there is a good way to do this, but I had something like this in mind... int i; for(i = 1; i < 4;...
30
by: Ivan Reborin | last post by:
Hello everyone, I was wondering if anyone here has a moment of time to help me with 2 things that have been bugging me. 1. Multi dimensional arrays - how do you load them in python For...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.