woh do I read a file with std::istream ???

A

Anne-Marte

Hi
I simply don't understand how to read a simple file using
std::istream. How do I open a file for reading with istream??

Anne-Marte
 
S

SirMike

Anne-Marte said:
I simply don't understand how to read a simple file using
std::istream. How do I open a file for reading with istream??
Maybe std::ifstream will be better for you ?
 
T

Thomas Matthews

Anne-Marte said:
Hi
I simply don't understand how to read a simple file using
std::istream. How do I open a file for reading with istream??

Anne-Marte

Look up ifstream. (Note the 'f'.)
The generic method is:
1. Open the file.
2. Extract data from the stream.
3. Close the file when finished.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
G

Grant

Anne-Marte said:
Hi
I simply don't understand how to read a simple file using
std::istream. How do I open a file for reading with istream??

Anne-Marte

try this:

#include <iostream>
#include <fstream>

using std::cout;
using std::endl;
using std::ifstream;
using std::streamsize;
using std::ios;

int main()
{
ifstream in("C:\\temp.txt"); // Opens file

char str[80]; // Sets up string buffer
while(in.good())
{
in.getline(str, 80); // Gets 80 chars and stores them in str
streamsize n = in.gcount(); // Counts how many chars have been read
cout << str << endl << n << endl; // Prints them to the screen
}

cout << "Bad bit set: " <<( in.rdstate( ) & ios::badbit ) << endl;
cout << "Fail bit set: " <<( in.rdstate( ) & ios::failbit ) << endl;
cout << "End of file bit set: " << ( in.rdstate( ) & ios::eofbit ) <<
endl;
cout << endl;

in.close(); // Closes file
return 0;
}

This will read your file in text mode. It will attempt to read all the
characters in a line until the new line character is found and then
transfer this to the buffer str. This buffer is defined as 80 lines
long so if the line is 80 characters or longer the stream will place 79
of the characters in the buffer str and add the NULL charcter '\0' at
the end. If a line is 80 characters or longer the failbit will be set.
This means it will exit from the loop. If you want to continue the
loop going then add this to the end of the while loop.

if (in.fail())
in.clear()

This will reset the stream to continue.

Hope this information helps. Please note there is more than one way to
read a file using ifstream.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top