In this post we will write a C++ program to copy string into file and then read it and display on screen.

C++ program to copy string into file and then read it and display on screen


C++ program to copy string into file and then read it and display on screen.

#include<iostream>
#include<conio.h>
#include<fstream>
#include<process.h>
#include<string.h>
using namespace std;

int main()
{
  system("cls");

    char ch;
    char str[] = "If you want to accomplish your dream then wake up";
    ofstream add("Text.txt");
        if(!add)
        {
            std::cout<<"File opening error";
            getch();
            exit(0);
        }

    int m = strlen(str);
    for(int j=0; j<m; j++)
    {
        add.put(str[j]);
    }

    add.close();

    ifstream ss("Text.txt");
        if(!ss)
        {
            std::cout<<"File opening error";
            getch();
            exit(0);
        }
    while(ss)
    {
        ss.get(ch);
        std::cout<<ch;
    }

    getch();
    return 0;
}



Output

If you want to accomplish your dream then wake up