Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

read text from file c++

fstream input;
	input.open("Data.txt", ios::in);
	string city;
	if (input.fail())
	{
		cout << "File does not exist" << endl;
		cout << "Exit program" << endl;
	}

	while (!input.eof()) // Continue if not end of file
	{
		getline(input, city, '.');//getline(file,string,delimit char)
		cout << city << endl;
	}

	input.close();

                            //Another solution

fstream file2;
	file2.open("Data.txt", ios::in);
	string line;
	cout << "Data file: 
";
	if (file2.is_open()) {		//check if the file is open
      
		while (getline(file2, line)) {
			cout << line << endl;
		}
      	file2.close();
	}
 
PREVIOUS NEXT
Tagged: #read #text #file
ADD COMMENT
Topic
Name
7+4 =