In this post we will write a C++ program to create a file in text mode and write then read data from file.

C++ program to create a file in text mode and write then read data from file


C++ program to create a file in text mode and write then read data from file.

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

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

    char ch = 'a';
    float a = 25.5;
    int b = 50;

    ofstream add("Test.txt");

    if(!add)
    {
        std::cout<<"File opening error";
        getch();
        exit(0);
    }

    add<<ch;
    add<<a;
    add<<b;
    add.close();

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

    ss>>ch;
    ss>>a;
    ss>>b;

    std::cout<<ch<<std::endl;
    std::cout<<a<<std::endl;
    std::cout<<b<<std::endl;

    getch();
    return 0;
}



Output

a
25.549999
50