Write a program in C++ to add text into a File - WAY2IT

Latest Post :

Home Top Ad

Write a program in C++ to add text into a File



Code :-

#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char string[80];
cout << "Enter a String \n";
gets(string);
int len = strlen(string);
fstream file;
file.open("Test.txt",ios::out | ios::in);
for(int i=0; i<len; i++)
file.put(string[i]);
file.seekg(0); //Moves get pointer(input)to a specified location.
char ch;
while(file)
{
file.get(ch);
cout << ch;
}
file.close();
cout << "\nData stored Successfully";

getch();
}


Output :-