Jump to content
• Advertisement

#### Archived

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

# C++ Help?

This topic is 6063 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## 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

##### Share on other sites
Advertisement
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 //note: no .h on the end//you might have to include iostream or something like thatofstream 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

##### Share on other sites
Ok Thanks you.
One more question. How should I write the .dat file. Should I write it like it were a text file, or should I write it in C++? What?

#### Share this 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

##### Share on other sites
If that .dat file contains text your instructor needs a good solid kick in the arse.

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

##### 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

Thank you much

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
21
4. 4
5. 5
• Advertisement

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631778
• Total Posts
3002310
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!