Archived

This topic is now archived and is closed to further replies.

Vegadam

C++ Help?

Recommended Posts

Ok I need to know how to open a dat file and read it, and then print its contents. Im writeing a small project for class. What I need to do is have it open the file read it, to find out who is online, and if they are active. Then print them in a Two Dimensional arry. Of course these are just made up people and it is not online. So what Im asking is how do I write the .dat file? Thanks for the help, Adam.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
do you know how to use cout? cout is an output stream that sends data to the console. What you want is a stream that sends data to a file. To make such a file:
  
#include <fstream>//note: no .h on the end
//you might have to include iostream or something like that

ofstream outputfile("data.dat");


now use it just like you would use cout. You don''t have to worry about flushing or closing the file, when the object goes out of scope the destructor will do all that for you.

Share this post


Link to post
Share on other sites
I''m pretty rusty on binary IO using the streaming classes. I generally use the Win32 asyncronous functions...

I do recall that you need to specify ios::bin or ios::binary when opening the file.

Magmai Kai Holmlor

"Oh, like you''ve never written buggy code" - Lee

"What I see is a system that _could do anything - but currently does nothing !" - Anonymous CEO

Share this post


Link to post
Share on other sites
Well, since this is a class project I'm not going to help you entirely; just point you in the right direction. I don't really understand your last comment, but I'll try to answer it.

There are two basic ways of writing files: binary and ASCII. For now, just deal with ASCII (binary is more complicated for newbies). There is also UNICODE which is somewhat like an extension of ASCII that provides support for LOTS of different characters.

ASCII stand for (correct me if I'm wrong) American Standard Code for Information Interchange. Basically it's just a bunch of characters such as a-z, A-Z, +,-,*,/,#,etc. as well as some "special" characters such as /n etc.

So, when you write in your favorite editor (i.e. vi, MS Word, AbiWord, Notepad, etc.) you are entering ASCII characters.

If you have the people's names etc. already in hand just open up notepad, vi, or some other basic editor and write them. You could make your own program to do this, but that's overkill.

Then in your program make a loop that continues till it reaches the end of the file, and keeps getting peoples names from the list then moves down one line. You may want to use getline() if you want their entire name (since when you do fin >> blah; it will only get the text until a space ' ' is reached).

SUMMARY: Just write your data in an ASCII text editor then write your code to receive input from the file stream.

Hopefully I helped!


EDIT: IF your instructor made you do it in binary then I think the arguement you need in your constructor is ios::bin. Binary is essentially really easy; it's just that you can't read it (unless you can read binary, like 'A' is 01000001

Edited by - Floppy on December 15, 2001 10:54:14 PM

Share this post


Link to post
Share on other sites