Search
 
SCRIPT & CODE EXAMPLE
 

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();
	}
Comment

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();
	}
Comment

PREVIOUS NEXT
Code Example
Cpp :: Increase IQ codechef solution in c++ 
Cpp :: c++ to c code converter online 
Cpp :: cpp class access array member by different name 
Cpp :: Qt asynchronous HTTP request 
Cpp :: contains in c++ map 
Cpp :: c++ sort numbers by magnitude/absolute value 
Cpp :: Write a CPP program to calculate sum of first N natural numbers 
Cpp :: sleep function i nc++ 
Cpp :: can you add a bool and an int 
Cpp :: code::block uncomment 
Cpp :: solve diamond inheritance c++ 
Cpp :: easy way to encrypt a c++ file line by line 
Cpp :: gcc compile multi thread 
Cpp :: c++ thread id 
Cpp :: Diamond pattren program in C++ 
Cpp :: inorder to postorder converter online 
Cpp :: convert c program to c++ online 
Cpp :: inside information subtask 2 
Cpp :: esp8266 wifi.localip() to string 
Cpp :: 1047. Remove All Adjacent Duplicates In String solution leetcode in c++ 
Cpp :: assignment of single field in struct in solidity 
Cpp :: C++ thread header 
Cpp :: how to complie c++ to spesific name using terminal 
Cpp :: cpp full form 
Cpp :: find the second aperrence of a char in string c++ 
Cpp :: vprintf 
Cpp :: C++ Things to Remember 
Cpp :: prime template c++ 
Cpp :: how to get the numbers in a vector c++ sfml 
Cpp :: c++ remove last element from array 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =