File Streams in C++ read from a file and Write to a file

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

int main()
{
    fstream fStream{"read.txt"};
    string line;
    while (fStream)
    {
        getline(fStream, line);
        cout << line << endl;
    }

    fstream f{"read.txt"};
    char c;
    while (f >> noskipws >> c)
    {
        cout << c;
    }

    fstream fs{"out.txt", ios::app};
    string s = "Hello World!\n";
    string s2 = "Some Text\n";
    /*
    fs <<"First Line."<<'\n';
    fs <<"Second Line."<<'\n';
    fs <<"Third Line."<<'\n';

    fs <<"Line 4"<<'\n';
    */
    fs << s << s2;
    return 0;
}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *