C++ Programming

Write File

C plus plus example for write file using file stream

11/28/2019
0 views
write-file.cppCPP
#include<iostream>
#include<fstream>
using namespace std;

int main() {
	int item_no;
	string item_name;
	float price;

	/* Connect 'data.txt' to 'wfile' */
	ofstream wfile("data.txt");
	cout << "Enter data to write on the file" << endl;
	cout << "Item no:" << endl;
	cin >> item_no;

	/* Write integer in the file */
	wfile << item_no << endl;

	cout << "Item name:" << endl;
	cin >> item_name;

	/* Write string in the file */
	wfile << item_name << endl;

	cout << "Price:" << endl;
	cin >> price;

	/* Write float in the file */
	wfile << price << endl;

	/* Disconnect 'data.txt' from 'wfile' */
	wfile.close();
	cout << "File written successfully!" << endl;
	return 0;
}


/* Output */
Enter data to write on the file
Item no:
50

Item name:
Book

Price:
20.12

File written successfully!


/* 'data.txt' look like */

50
Book
20.12
cppwrite filefile stream

Loading comments...

Related Examples

Deliver breaking news, insightful commentary, and exclusive reports.

Targeting readers who rely on our platform to stay ahead of the curve.

Contact Us: benzingaheadlines@gmail.com