Logging using ofstream

How to log anything that is put into/out of the program.

Page 1 of 1

11 Replies - 4229 Views - Last Post: 15 October 2001 - 07:32 AM Rate Topic: -----

#1 VxJasonxV  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 201
  • Joined: 03-October 01

Logging using ofstream

Posted 11 October 2001 - 10:20 PM

Well...I just set up a record keeper for my craps program, now I need to learn how to append to it so I can have a list of scores.  Anyways, since it is required to have everything that happens logged, I'm sorta stuck.  The only way I know how to write to a file is directly.
outfilename << variable << "or direct text.
";

But do to an interesting event today in 3rd block, we can't just double every cout line with a outfile line.  Any suggestions or hints?

Is This A Good Question/Topic? 0
  • +

Replies To: Logging using ofstream

#2 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4510
  • View blog
  • Posts: 28,414
  • Joined: 21-March 01

Re: Logging using ofstream

Posted 12 October 2001 - 01:47 PM

outfiles work like this

first, your declare like this

ofstream outfile;

outfile can be replaced with any other thing you want, thats just what i always use.

next you have to open it

outfile.open("outfile.txt", ios::out);

the outfile.open part, needs to be the identifier you made to ofstrem, fo if you put ofstream out; then its out.open instead.

and before the .txt you can put any file name you want, and you can make it a .dat or a .txt if you want, or whatever you want, the program will read it.

next, is writing to the actual outfile

outfile << "w00t
";

use it like a normal cout, just replace cout, with whatever identifier you used, in this case, outfile.

there, you go, thats how its done. :)

(Edited by supersloth at 3:50 pm on Oct. 12, 2001)

Was This Post Helpful? 0
  • +
  • -

#3 Cookie Mobster  Icon User is offline

  • nooneenooneenooonee
  • member icon

Reputation: 7
  • View blog
  • Posts: 4,730
  • Joined: 12-October 01

Re: Logging using ofstream

Posted 12 October 2001 - 03:01 PM

I would store to the file in binary format rather than in ascii. I can't remember exactly how to do it with the fstream library but it is something like

ofstream out;
out.open("filename",ios::binary);
out.write("data",BIT_LOCATION);
out.close();

read from the file by using the ifstream object.

ifstream in;
in.open("name",ios::binary);
in.read(char [] data,BIT_LOCATION,BIT_READ_SIZE);
in.close();

this way is a little more stable and you don't have to write the whole file every time you write any data.
P.S. remember to chack the length so you don't overflow any buffers and crash your program, or corrupt the save file.

Cheers Sam

Was This Post Helpful? 0
  • +
  • -

#4 VxJasonxV  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 201
  • Joined: 03-October 01

Re: Logging using ofstream

Posted 12 October 2001 - 04:09 PM

I know how to write to the file...I've already done it.  (And I don't need to use binary...cause all I'm outputting is basic text...but thanks anyway).
What I need to know is how to log my ENTIRE program, anything that goes into/comes out of the program has to be logged.  but I'm not about to do:
cout << "Enter yes or no.
";
recordout << "Enter yes or no.
";
cin.get(ans, 4);
recordout << ans[4] << '
';

Do you get what I'm saying?
Like...using programs like FTP pro...or installation programs...they make a log file containing EVERYTHING that happened, every action that took place.  I'm not going to double up on code cause that will cause hanosh to jump all over me.
There has to be a way to do that without doubling up on code...isn't there a way?  Please tell me there's a way.
Was This Post Helpful? 0
  • +
  • -

#5 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4510
  • View blog
  • Posts: 28,414
  • Joined: 21-March 01

Re: Logging using ofstream

Posted 12 October 2001 - 04:15 PM

i hate to break it to you, ive been complaining about that since i started programming, the way you coded, its the only way, but if someone knows another way, please let me know.
Was This Post Helpful? 0
  • +
  • -

#6 malkiri  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 364
  • Joined: 29-March 01

Re: Logging using ofstream

Posted 12 October 2001 - 06:37 PM

What you might consider doing is making a new class, and overload the << operator. Inside the operator<< function, you would both output to cout and to the file.
Was This Post Helpful? 0
  • +
  • -

#7 runtime error  Icon User is offline

  • Lucky.Code
  • member icon

Reputation: 3
  • View blog
  • Posts: 629
  • Joined: 19-March 01

Re: Logging using ofstream

Posted 12 October 2001 - 06:55 PM

Quote

Quote: from malkiri on 9:37 pm on Oct. 12, 2001
What you might consider doing is making a new class, and overload the << operator. Inside the operator<< function, you would both output to cout and to the file.

Well that would work except classes are too advanced for them right now.

Was This Post Helpful? 0
  • +
  • -

#8 malkiri  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 364
  • Joined: 29-March 01

Re: Logging using ofstream

Posted 12 October 2001 - 11:23 PM

Well, in that case, what about a simple function? It could take an array of strings and do the output to cout and the file inside it.

-Malkiri

Was This Post Helpful? 0
  • +
  • -

#9 VxJasonxV  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 201
  • Joined: 03-October 01

Re: Logging using ofstream

Posted 13 October 2001 - 08:18 AM

I'm thinking about that...but I don't understand how that would work.
Was This Post Helpful? 0
  • +
  • -

#10 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4510
  • View blog
  • Posts: 28,414
  • Joined: 21-March 01

Re: Logging using ofstream

Posted 13 October 2001 - 01:16 PM

you would basically copy and paste all of your couts, and the data inputed with cins into a new function that does nothing but outfile.

but, in your case, since this is the game craps, i dont think that would work out well, cause you dont know how many times they are playing, and used the same variables for every loop.

also, are you sure hanosh wants everything put into an outfile? last year, i usually only put the most important things into an outfile.

Was This Post Helpful? 0
  • +
  • -

#11 VxJasonxV  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 201
  • Joined: 03-October 01

Re: Logging using ofstream

Posted 13 October 2001 - 01:33 PM

I dunno what he wants exactly...Eric output EVERYTHING by doubling the code (cout << "blah
";, output << "blah
";)
Then I asked him if I could just make a record keeper...he said no.
Was This Post Helpful? 0
  • +
  • -

#12 malkiri  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 364
  • Joined: 29-March 01

Re: Logging using ofstream

Posted 15 October 2001 - 07:32 AM

Well, find out what he wants then! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1