| | |
Trouble with printing to a file
Thread Solved |
•
•
Join Date: Jul 2005
Posts: 24
Reputation:
Solved Threads: 0
Hello, I'm almost done with my program however I'm having trouble getting what I want printed to a file. I'm familiar with the printf function and what I want is to print out to the file in scientific notation, which would be %E if I were using printf. Is there something similar when writing out to a file. Also when using printf you can choose how many decimal places you need using %.2f or something like that, I also need to use something similar to that when writing out to a file. Here is how I have been writing out to a file. Can someone please help.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> #include <fstream> #define MINMASS (0.0000515053396) #define pi (3.14159265) #define JUPVOL (6.98509E9 * 6.98509E9 * 6.98509E9 * (4.00/3.00) * pi) #define SOLM2G (1.98892E33) using namespace std; int main () { double j1, j2, j3, j4; ofstream file; file.open("big.in"); //open a file srand ( time(NULL) ); srand48(time(NULL)); j1 = drand48() * 0.013365; //mass needs to be between 5.15053396E-5 and 0.013365 while(j1 < MINMASS) { srand48(time(NULL)); j1 = drand48() * 0.013365; } file<<" JOVIAN1 m="<<j1<<" r=20.D0 d="<<(j1/JUPVOL)*SOLM2G ; file.close(); //close it return 0; }
functions in <ioman>. Here are other values that can be used for setioflags function.
C Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; int main() { float n = 123.456F; // output float with 5 decimal places cout << setprecision(5) << setiosflags(ios_base::fixed) << n << endl; return 0; }
•
•
Join Date: Jul 2005
Posts: 24
Reputation:
Solved Threads: 0
Thanks for your reply, I changed my code with your suggestion and at first I thought it was working the way I wanted, but it didn't. The first value that was written out seems fine, the second value that gets printed out seems to be modified by the specification of the first. What I am expecting to get is:
JOVIAN1 m=9.26272836047433573E-03 r=20.D0 d=13.72
using: what I get is: JOVIAN1 m=9.26272836047433573e-03 r=20.D0 d=13
The second value doesn't have two decimal places. The smaller problem, and it may be a little picky is when using printf %E gives you an upper case E for the exponential, is there any way of doing this? I'm going to run this file into a simulation program and the format for the simulation shows that it uses an upper case E. Thanks again.
JOVIAN1 m=9.26272836047433573E-03 r=20.D0 d=13.72
using:
C Syntax (Toggle Plain Text)
file<<" JOVIAN1 m="<< setprecision(17) << setiosflags(ios_base::scientific) <<j1<<" r=20.D0 d=" << setprecision(2) << setiosflags(ios_base::fixed) <<(j1/JUPVOL)*SOLM2G;
The second value doesn't have two decimal places. The smaller problem, and it may be a little picky is when using printf %E gives you an upper case E for the exponential, is there any way of doing this? I'm going to run this file into a simulation program and the format for the simulation shows that it uses an upper case E. Thanks again.
•
•
Join Date: Jul 2005
Posts: 24
Reputation:
Solved Threads: 0
I tried changing the precision, but what happens is if the random number is greater than nine, it alters the number of decimal places. So if it's set for 2 and the number is 10.XXXX it only prints out 10 If the number is 9.XXXXX it only prints out 9.X. I don't know what's going on here. I thought set precision was supposed to only effect the numbers after the decimal place.
Yeah, it's a program that randomly generates the mass of a jovian planet from Neptunes mass up to 14 times Jupiters mass, and from the mass it calculates the density of the planet. Then I'll run the file in a simulation and see which orbits are stable. It's actually pretty cool, I just have to have to get everything up and running correctly.
Yeah, it's a program that randomly generates the mass of a jovian planet from Neptunes mass up to 14 times Jupiters mass, and from the mass it calculates the density of the planet. Then I'll run the file in a simulation and see which orbits are stable. It's actually pretty cool, I just have to have to get everything up and running correctly.
•
•
Join Date: Jul 2005
Posts: 24
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Ancient Dragon
for floats look again at the cout line in my original post.
C Syntax (Toggle Plain Text)
file<<" JOVIAN1 m="<< setprecision(17) << setiosflags(ios_base::uppercase) << setiosflags(ios_base::scientific) << j1 <<" r=20.D0 d=" << flush << setprecision(2) << setiosflags(ios_base::fixed) << ((j1/JUPVOL) * SOLM2G) << endl;
Even when I do use endl at the end of each output separating them by line it doesn't give me a decimal with two places. If I don't have the first input at all it works just fine printing out two decimal places.
what is the desired output?
if that last number is what you want, then you have to remove the scientific flag. You can combine some of those setflags by using | operator.
Also -- srand() (and its variants) should only be called once at the very betinning of the program. you should remove it from that loop.
•
•
•
•
JOVIAN1 m=8.08181550000000040E+001 r=20.D0 d=112595.65
file<<" JOVIAN1 m="<< setprecision(17) << setiosflags(ios_base::uppercase | ios_base::scientific)
<< j1 <<" r=20.D0 d=" << resetiosflags(ios_base::scientific) << setprecision(2) << setiosflags(ios_base::fixed) << ((j1/JUPVOL) * SOLM2G) << endl;Also -- srand() (and its variants) should only be called once at the very betinning of the program. you should remove it from that loop.
![]() |
Similar Threads
- Redirecting console output to file (Java)
- Printing file access permission.... (Shell Scripting)
- reading and printing a file to screen in C (C)
Other Threads in the C Forum
- Previous Thread: replacing unprintable characters
- Next Thread: Pls help with C code. Evaluating data types.
Views: 2180 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C
.net api append array arrays binary binarysearch c++ char character code codeblocks coke command conversion convert curl database decimal directory dude dynamic engrish error exec execv factorial fflush fgets file floatingpointvalidation fork function functions givemetehcodez grade graphics homework i/o include input insert int integer intmain() keyboard lazy libcurl line linked linkedlist linux list lists loop malloc matrix memory mysql no-effort output overwrite path pipe pointer pointers prime problem process processing program programming read recursion recursive recv reverse scanf select server socketprograming special spoonfeeding string strings strtok structures student subscript system testing tftp turbo-c unbuffered unix user variable va_list windows windows-mobile






