Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > woh do I read a file with std::istream ???

Reply
Thread Tools

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

 
 
Anne-Marte
Guest
Posts: n/a
 
      03-30-2005
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
 
Reply With Quote
 
 
 
 
SirMike
Guest
Posts: n/a
 
      03-30-2005
Anne-Marte wrote:
> 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 ?

--
SirMike
the code is my strength
http://www.sirmike.grudziadz.com
 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      03-30-2005
Anne-Marte wrote:
> 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
 
Reply With Quote
 
Grant
Guest
Posts: n/a
 
      03-30-2005

Anne-Marte wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File.read(fname) vs. File.read(fname,File.size(fname)) Alex Dowad Ruby 4 05-01-2010 08:20 AM
file.read() doesn't read the whole file Sreejith K Python 24 03-24-2009 12:20 PM
How to read from a file, stored the data in an array, and return number of records read back to main tyeung168@gmail.com C++ 2 02-27-2007 07:12 AM
Database file and folder are not read only but error message says it is read only?? keithb ASP .Net 2 06-07-2006 03:40 PM
jython/python: read file and allow read to be terminated py Python 0 01-27-2006 08:41 PM



Advertisments