Sat Nov 30 2019

Read File

C++ Programming1855 views

File Name: read-file.cpp

#include<iostream>
#include<fstream>
using namespace std;

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

	/* Connect 'data.txt' to 'wfile' */
	ifstream rfile("data.txt");
	cout << "Read data from file" << endl;

	/* Read Integer */
	rfile >> item_no;

	/* Read string */
	rfile >> item_name;

	/* Read float */
	rfile >> price;
	
	cout << "Item No: " << item_no << endl;
	cout << "Item Name: " << item_name << endl;
	cout << "Price: " << price << endl;

	/* Disconnect 'data.txt' from 'wfile' */
	rfile.close();
	return 0;
}


/* Output */
Read data from file
Item no: 50
Item name: Book
Price: 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.