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

c++ how to read from a file

#include <fstream>
#include <iostream>

using namespace std;

int main() {
    /* You create a stream using the file in a 
     * similar way to how we use other streams.
     */
    ifstream in;
    // Open the file
    in.open("names.txt");
    if(in.fail())
        cout << "File didn't open"<<endl;

    int count = 0;
    string line;
    while (true) {
        /* Get line will work as long as there is
         * a line left. Once there are no lines 
         * remaining it will fail. 
		 */
        getline(in, line);  
        if (in.fail()) break;
        
        /* Process the line here. In this case you are
         * just counting the lines.
         */
        count ++;
    }
    
    cout << "This file has " << count << " lines." << endl;
    return 0;
}
Comment

read and write file in c++

#include<iostream>
#include<fstream>

using namespace std;

int main()
{

    string fileName;
    int choice;
    cout << "Enter txt file name:
";
    cin >> fileName;
    cout << "Enter number of command:
";
    cout << "1. Write file
";
    cout << "2. Read file
";
    cin >> choice;

    if (choice == 1)
    {
            ofstream myfile;
            string text;
            myfile.open (fileName);
            cout << "Write text below: 
"; //file must have no text
            cin >> text;
            myfile << text;
            myfile.close();
    }else if (choice == 2)
    {
            ifstream file(fileName);
            string line;

            if (file.is_open())
            {
	            while (getline(file, line))
                {
        	       cout << line << endl;
                }
    }
    }
    return 0;
}
Comment

How to read files in C++

#include <fstream> 

/*
ofstream 	Creates and writes to files
ifstream 	Reads from files
fstream 	A combination of ofstream and ifstream: creates, reads, and writes to files
*/

int main() {
  // Create and open a text file
  ofstream MyFile("filename.txt");

  // Write to the file
  MyFile << "Files can be tricky, but it is fun enough!";

  // Close the file
  MyFile.close();
} 
Comment

c++ read file

#include <stdio.h>

#define n 1024 // n bytes

int main(void) {
	FILE *fp;
    size_t numread;
    if ((fp = fopen("yourfile.xy", "rb") != NULL) {
    	char buffer[n];
        numread = fread(buffer, sizeof(*buffer), n, fp);
      	printf("Read %d Bytes: %s", numread, buffer);
      
      	fclose(fp);
      	return 0;
    }
        
    return -1;
}
Comment

cpp read from file

freopen("filename.extension", "r", stdin);
Comment

PREVIOUS NEXT
Code Example
Cpp :: swapping of two numbers 
Cpp :: c++ remove numbers from vector if larger than n 
Cpp :: functors in c++ 
Cpp :: what is c++ used for 
Cpp :: stl sort in c++ 
Cpp :: c++ vector declaration 
Cpp :: vector length c++ 
Cpp :: indexing strings in c++ 
Cpp :: c++ pause linux 
Cpp :: insert only unique values into vector 
Cpp :: c++ add two matrix 
Cpp :: who to include a library c++ 
Cpp :: opencv open image c++ 
Cpp :: cout c++ 
Cpp :: untitled goose game 
Cpp :: See Compilation Time in c++ Program 
Cpp :: unreal engine c++ 
Cpp :: cpp mutex 
Cpp :: C++ fill string with random uppercase letters 
Cpp :: c++ do every 1 minutes 
Cpp :: intersection.cpp 
Cpp :: c++ replace 
Cpp :: quick sort 
Cpp :: accumulate() in c++ 
Cpp :: c++ program to convert character to ascii 
Cpp :: vector in c++ 
Cpp :: c++ integer array 
Cpp :: lower bound and upper bound in c++ 
Cpp :: C++ Pi 4 Decimal 
Cpp :: clear previous terminal output c++ 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =