#### Archived

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

# File I/O in C++

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

## Recommended Posts

what files do I need to include ( io.h by any chance ? ) once I have the right header file included WHICH function/s would I need to use to read the data and how do I go about reading them?????, no need for creating classes or anything I just wanna see how to use the functions ( I have my own class with data to store what I read in ) thanks to anyone who can help ;D

##### Share on other sites
You should include fstream.
Like this:

  #include <fstream>

( note: yes, that is without the .h! Very important. )

Then you use the fstream object to stream data to and from file. For more information, look up streams, istream and ostream in MSDN or in your favourite C++ book.

( useful functions: the >> and << operators, and fstream::read, fstream::write, if I recall correctly ).

##### Share on other sites
Include <fstream>

Here is a reference to fstream. To read from a file look at the member functions inherited from istream.

The hackers must have gotten into the system through the hyperlink!!

##### Share on other sites
hey dudes thanks alot ;D

##### Share on other sites
Here''s a quick example:

  #include <fstream>int main(){  ifstream file("data.txt"); // open data.txt for input  int num;  while(file >> num) { // keep reading integers from the file    /* do something with num */  }  if(!file.eof()) {    /* found some data that wasn''t an integer */  }  return 0; // file is closed automatically}

Here, data.txt should contain integers (in a decimal representation), separated by whitespace.

##### Share on other sites
ok guys thanks but what if I want to read different types of data from a file, lets say I wanted to read bytes in aswell as char''s?????? how would I do that???

##### Share on other sites
Stealth:

You probably should stick to binary if you want bytes as well as characters. Here's an example for input:

ifstream infile("file.dat", ios::in | ios::binary);

If the file was filled with, say, 10 Object objects, you could read them like this:

  for (x = 0; x < 10; x++) {      infile.read((char *) &ObjectHolder[x], sizeof(Object));}// for

and for output:

  ofstream outfile("file.dat", ios::out | ios::binary);for (x = 0; x < 10; x++) {       outfile.write((char *) &ObjectHolder[x]. sizeof(Object));}// for

John.

[edited by - JohnAD on August 27, 2002 12:07:34 PM]

##### Share on other sites
quote:
( note: yes, that is without the .h! Very important. )

*starts to have a nervous breakdown*

Lately, I''ve been seeing lots of people move to headers without the ''.h''. Why is this? Are they different files, and what added functionality does this give?

I learned C++ a while ago, and this syntax change is giving me a headache.

##### Share on other sites
The headers without the .h are the new standard library headers. They introduce new functions, and the declarations are placed in a namespace (std). That was not the case with the old headers (they were in global namespace).

The old .h headers are only for compatibility with old code, and should not be used anymore. The reason that there is no .h on the new files, is that the standarization commitee needed to find a way to include new headers, but without changing the names or creating ambiguities. They agreed upon just dropping the .h suffix.

/ Yann

[edited by - Yann L on August 27, 2002 2:41:57 PM]

##### Share on other sites
For more detailed information regarding File I/O:
http://www.cpp-home.com/FileIO_tutorial.php

Loobian,
www.cpp-home.com ; C++ Resources

##### Share on other sites
You can do file I/O the old fashioned C way. It is simpler but less effective. For this you can use the "io.h" and the "stdio.h" headers. These have file I/O commands such as fread(), fprintf(), etc.... The list is quite plentiful. However you do not have the convenience of the iostream as you do if you use fstream but if you want it simpler but less effective then it could be a good choice.

## And the Dark Lord shall DESTROY Middle Earth

##### Share on other sites
If you''re going 2 use the

without the ".h"
then you need 2 include this 2

namspace std;

Because since you''ll be using the newer way 2 list headers in your .cpp files, you have 2 include the "namespace std;" too if you don''t want 2 add the ".h" at the end of header files. Also some header files may have been renamed when c++ when from the old header naming system 2 the namespace system.