Thread: ifstream and ofstream

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    12

    ifstream and ofstream

    I'm reading this tutorial:
    http://www.cprogramming.com/tutorial/lesson10.html

    I have found this a little bit challenging. Can anyone expound to me what it means technically.
    I'm a python programmer and would like to add C++ and take advantage of ctypes and also I learn myself.

    Thanks for your help.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, could you be a bit more specific as to which part of the tutorial you do not understand?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    How good are you with cin and cout? File streams are supposed to work pretty much exactly the same way.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    12
    In python it is as simple as:
    fo = open("steve.txt")
    for words in fo:
    print words

    it will print all words in text files. I was wondering how easy it is in C++

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Soma

    Code:
    #include <fstream>
    #include <iostream>
    
    int main()
    {
       return(!(std::cout << std::ifstream("test.cxx").rdbuf()));
    }

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, that is the equivalent of

    Code:
    print open("test.cxx").read()
    in Python.

    Use getline(input_stream_object, string_object) to read a line of input, just as you would do with cin.

    Code:
    ifstream fin("test.cxx");
    std::string line;
    while (std::getline(fin, line)) {
        std::cout << line << '\n';
    }
    Last edited by anon; 02-25-2009 at 02:35 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    12

    Thumbs up

    Plus your help, this helped a lot
    http://www.cplusplus.com/doc/tutorial/files.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ofstream... broke as a result of ifstream fix
    By tms43 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2006, 11:40 AM
  2. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 08:34 AM
  3. ifstream and ofstream
    By Stan100 in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2002, 04:53 PM
  4. Replies: 9
    Last Post: 06-06-2002, 07:03 PM
  5. STL to: Cin, Cout, ifstream & ofstream
    By kuphryn in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2001, 09:32 AM