Search
 
SCRIPT & CODE EXAMPLE
 

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();
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: log base 10 c+_+ 
Cpp :: error c4001 site:docs.microsoft.com 
Cpp :: what is c++ 
Cpp :: prompt user for bool statement C++ 
Cpp :: cpp pass function with input to thread 
Cpp :: kruskal algorithm 
Cpp :: c++ stack 
Cpp :: switch cout print with a prameter c++ 
Cpp :: define for loop c++ 
Cpp :: long, long long 32 bit or 8 bit in c++ 
Cpp :: beecrowd problem 1001 solution in c++ 
Cpp :: pimpl c++ 
Cpp :: check whether kth bit is 1 
Cpp :: zsh: segmentation fault ./provided_files.exe erosion X . 
Cpp :: hackerearth questions siemens 
Cpp :: number of characters in string 
Cpp :: c++ scanf always expects double and not float 
Cpp :: c/c++ pointers 
Cpp :: c++ abs template 
Cpp :: fibonacci sequence c++ 
Cpp :: i++ and++i 
Cpp :: Maximum Cake Tastiness codeforces solution 
Cpp :: 191. Number of 1 Bits leetcode solution in c++ 
Cpp :: fasdf 
Cpp :: different way to print string in c++ 
Cpp :: binary algebra cpp 
Cpp :: C++ thread header 
Cpp :: c++ hsl to rgb integer 
Cpp :: arrays to function c++ 
Cpp :: calculate number of edges of graph in data structure c++ 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =