ifstream/ofstream

This is a discussion on ifstream/ofstream within the C++ Programming forums, part of the General Programming Boards category; #include <string> #include <fstream> using namespace std; int main() { ifstream in("Scopy.cpp"); // Open for reading ofstream out("Scopy2.cpp"); // Open ...

  1. #1
    Perverted unanimous's Avatar
    Join Date
    Oct 2001
    Posts
    336

    Post ifstream/ofstream

    #include <string>
    #include <fstream>
    using namespace std;

    int main() {
    ifstream in("Scopy.cpp"); // Open for reading
    ofstream out("Scopy2.cpp"); // Open for writing
    string s;
    while(getline(in, s)) // Discards newline char
    out << s << "\n"; // ... must add it back
    return 0;
    }


    in this short program I would like to know how the newline character is discarded in the while loop by using getline(in,s).
    Give me a bad reputation!!!

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    '\n' is the default argument for the delimiter. If you don't specify your own, the newline will always be discarded.

    If you're just using streams to copy files, this is simpler -

    Code:
    #include <fstream> 
    using namespace std; 
    
    int main() { 
    
    	ifstream in("Scopy.cpp"); // Open for reading 
    	ofstream out("Scopy2.cpp"); // Open for writing 
    
    	out << in.rdbuf();
    
    	return 0; 
    }
    zen

  3. #3
    Perverted unanimous's Avatar
    Join Date
    Oct 2001
    Posts
    336

    Post thanks

    THANKS!!!
    Give me a bad reputation!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ifstream/ofstream to save variables
    By Donarstan in forum C++ Programming
    Replies: 4
    Last Post: 10-01-2008, 02:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21