473,666 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_fil e_processing_fi le_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(&r ecord);
fprintf(fpointe r,"%4d %f %c\n",record);
printf (" \nADD ANOTHER RECORD ----> ( y / n ) : ");
scanf("\n");
scanf("%c",&ano ther);
} while (another=='y');
return 0;
}
input_record(re c)
struct name *rec;
{
printf("\nEnter int___member1: ");
scanf("%4d",&(* rec).int___memb er1);
printf("\nEnter float_member2: ");
scanf("%f",&(*r ec).float_membe r2);
printf("\nEnter char__member3: ");
scanf("\n");
scanf("%c",&(*r ec).char__membe r3);
}
Nov 14 '05 #1
2 9523
hp******@yahoo. com (hp******@yahoo .com) writes in comp.unix.solar is:
|struct name {
|int int___member1;
|float float_member2;
|char char__member3;
|};
|fprintf(fpoint er,"%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(fpointe r,"%4d %f %c\n", record.int___me mber1,
record.float_me mber2, record.char__me mber3);

--
_______________ _______________ _______________ _______________ ____________
Alan Coopersmith al***@alum.calb erkeley.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_fil e_processing_fi le_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(fpoint er,"%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(fpointe r, "%4d %f %c\n",
record.int___me mber1,
record.float_me mber2
record.char__me mber3);
printf (" \nADD ANOTHER RECORD ----> ( y / n ) : ");
scanf("\n");
scanf("%c",&an other);
} while (another=='y');
return 0;
}
input_record(r ec)
struct name *rec;
Why are you still using this obsolete style for a function definition?
{
printf("\nEnte r int___member1: ");
scanf("%4d",&( *rec).int___mem ber1);
The -> operator is much preferred over the convoluted dereference
syntax you have:
scanf("%4d",&re c->int___member1) ;
printf("\nEnte r float_member2: ");
scanf("%f",&(* rec).float_memb er2);
printf("\nEnte r char__member3: ");
scanf("\n");
scanf("%c",&(* rec).char__memb er3);
}


<<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
3291
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 ' 2' Now, with a mapping....
1
530
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: 378 Child Tickets Sold: 127 Gross Box Office Profit: $ 2673.00 Net Box Office Profit: $ 534.60 Amount Paid To Movie C.:$ 2138.40
3
2486
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); // output the normal fprintf(fptr, "%f %f %f ", v1, v1, v1); // output the vertice fprintf(fptr, "%f %f %f ", normals, normals, normals); // output the normal
16
2549
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 is this happening, and how do I get rid of it? (using Dev-C++
122
5280
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) { /* array differs from value, do something*/
43
6560
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 positive integers or a decimal point. For example: Enter amount: and was capturing the float varialbe as in: scanf ("%f", &myVar)
6
1880
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 called: tempInput.txt, and then the information is calculated, and it is output on a different file called tempOutput.txt. The tempInput.txt containg those numbers as follows:
111
4624
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; i++){
30
2708
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 example, if I had: ------- 1 2 3 4 5 6
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.