Thu Nov 28 2019

Write File

C++ Programming1494 views

File Name: write-file.cpp

#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
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.