Does anyone know how to modify this small program that I wrote in C++ so that for every cout statement there is a statement that writes the exact same information to the file accelfig.txt? I use this program to generate acceleration values and write them to accelfig.txt and then I read these acceleration values into another program that I wrote that numerically integrates these figures to calculate 0 to 60 mph and 1/4 mile times, speed at the end of the 1/4 mile, maximum hp, 60 mph to 0 braking distance etc. I use the free C++ compiler from http://www.bloodshed.net/dev/devcpp.html .
//CalcAccl1
#include < iostream.h >
#include < stdio.h >
// This program calculates 30 seconds worth of acceleration values, assumed
// to be in ft/s^2, and writes them to the file accelfig.txt.
main()
{ FILE *fp;
int counter;
double acceleration = 0, time = 0;
fp = fopen("accelfig.txt", "w");
for (counter = 0; counter <= 1600; counter++)
{
acceleration = time + 10;
fprintf(fp, "%20.10f\n", acceleration);
cout << "\nThe acceleration at " << time << " seconds is " << acceleration << '\n';
time = time + 0.0025;
}
for (counter = 1601; counter <= 4800; counter++)
{
acceleration = 14;
fprintf(fp, "%20.10f\n", acceleration);
cout << "\nThe acceleration at " << time << " seconds is " << acceleration << '\n';
time = time + 0.0025;
}
for (counter = 4801; counter <= 12000; counter++)
{
acceleration = 62 - 4*time;
fprintf(fp, "%20.10f\n", acceleration);
cout << "\nThe acceleration at " << time << " seconds is " << acceleration << '\n';
time = time + 0.0025;
}
fclose(fp);
}
here you go:
Attached File(s)
accelfig.c ( 1.29k )
Number of downloads: 7
I get an error message when I try to open it with my compiler.
I tried the code and it works fine as the data goes into the file looks the same as what is on the screen. There is only the numeric data, no text, if that is what you want? I used Microsoft Devstudio 6.0 and it went fine?
all you need to do is to put this line in your code after each line of cout:
fprintf(fp, "\nThe acceleration at %f seconds is %f\n", time, acceleration);
back to work ...
edit: i did not try to compile it, i assumed you had it running BEFORE you posted here ...
Went fine for me also..
Alfred... he wouldnt post a virus infected on his own server for everyone on the BBS to dowload. Think past the anger ...
B
Ok, I copied and pasted that one line into my program and it seems to work perfectly. Thanks, Andy, I appreciate it.
If you want to use ostream fiiles the same way you use cout, you have to do a few includes, and set the output stream up. Once set up, you use it just like cout, except it's heading to a file.
Or, if you want the mundane approach, you could always switch back to C. Oops, I see you have. :-)
#include
#include
#include
main()
{
// open a file for outpout
ofstream oFile("newoutput.txt", ios::out);
// check to make sure it worked
if ( !oFile ) { //open failed?
cerr << "open failed" << endl;
exit (-1);
}
// write something to it
oFile << "The horsepower at " << "etc" << endl;
return 0;
}
This worked on GCC last time I tried it. (does it look familar? :-)
love kim.
hmm.. tabs disappear on posting. Interesting.
kim.
Buncha computer geeks! I use to know that crap. Not any longer.
Have fun!
Karl
So the <>'s appear in quoted text to me, but not in "regular" text.
Anyone else seeing this?
kim.
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)