Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++ fstream read line write ,creat file program

#include<fstream>
using namespace std;
void write_file();
void write_file(string s);
void read_file();
int main()
{
	string msg;
	cout<<"Enter your message";
	getline(cin,msg);
	write_file("Costum message");
	write_file();
	read_file();
	return 0;
}
void read_file()
{
	string s;
	ifstream file;
	file.open("myfile.text");
	if(file.is_open())
	{
		while(!file.eof())
		{
			file>>s;
			cout<<s<<" ";
		}
	}
	else
	{
	cout<<"File Not found";
	}
	file.close();
}
void write_file()
{
	ofstream file;
	file.open("myfile.text",ios::app);
	if(file.is_open())
	{
		file<<"
Hello i am a file: "<<endl;
		cout<<"File written";
	}
	else
	{
		cout<<"File couldnt open"<<endl;

	}file.close();
}
void write_file(string s)
{
	ofstream file;
	file.open("cmyfile.text",ios::app);
	if(file.is_open())
	{
		file<<s;
		cout<<"File writtn";
		cin>>s;
	}
	else
	{
		cout<<"File couldnt open"<<endl;

	}file.close();
}
 
PREVIOUS NEXT
Tagged: #fstream #read #line #write #file #program
ADD COMMENT
Topic
Name
7+7 =