Read File

C++ Programming

C++ Programming Examples

#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

Learn more about Input/Output in C++