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

Discussion in 'C++' started by Anne-Marte, Mar 30, 2005.

  1. Anne-Marte

    Anne-Marte Guest

    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
     
    Anne-Marte, Mar 30, 2005
    #1
    1. Advertisements

  2. Anne-Marte

    SirMike Guest

    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
     
    SirMike, Mar 30, 2005
    #2
    1. Advertisements

  3. 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
     
    Thomas Matthews, Mar 30, 2005
    #3
  4. Anne-Marte

    Grant Guest

    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.
     
    Grant, Mar 30, 2005
    #4
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    6
    Views:
    3,928
    Steve
    May 13, 2004
  2. Peter Jansson
    Replies:
    5
    Views:
    6,969
    Ivan Vecerina
    Mar 17, 2005
  3. Gernot Frisch

    from std::string to std::istream?

    Gernot Frisch, Mar 17, 2005, in forum: C++
    Replies:
    4
    Views:
    16,376
    Victor Bazarov
    Mar 18, 2005
  4. Jason K
    Replies:
    6
    Views:
    4,286
    Jeff Flinn
    May 12, 2005
  5. Vinu
    Replies:
    4
    Views:
    538
    Jim Langston
    Jul 7, 2005
  6. Gianni Mariani

    std::istream::readsome

    Gianni Mariani, Oct 28, 2006, in forum: C++
    Replies:
    13
    Views:
    2,876
    Gianni Mariani
    Oct 30, 2006
  7. xmllmx
    Replies:
    5
    Views:
    826
    Jorgen Grahn
    Jun 15, 2010
  8. mathieu
    Replies:
    4
    Views:
    797
    James Kanze
    Jul 28, 2013
Loading...