Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ write to file

// writing on a text file
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile ("example.txt");
    myfile << "This is a line.
";
    myfile << "This is another line.
";
    myfile.close();
  return 0;
}
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 write into 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

file handling read and write in c++

#include <iostream>
#include <fstream>

using namespace std;

int main(){
  fstream file;
  file.open ("example.txt", ios::out | ios::in );
  return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: time_t to int 
Cpp :: c++ how to read from a file 
Cpp :: struct and pointer c++ 
Cpp :: functors in c++ 
Cpp :: std distance 
Cpp :: sort index c++ 
Cpp :: C++ String Length Example 
Cpp :: Count Prefix of a Given String solution leetcode 
Cpp :: cstring to string 
Cpp :: cpp pushfront vector 
Cpp :: cudamemcpy 
Cpp :: how to split string in c++ 
Cpp :: sort a vector c++ 
Cpp :: c++ rand include 
Cpp :: reverse level order traversal 
Cpp :: how to print a text in c++ 
Cpp :: selection sort c++ algorithm 
Cpp :: classes and objects in c++ 
Cpp :: Find minimum maximum element CPP 
Cpp :: c++ check substring 
Cpp :: letter occurrence in string c++ 
Cpp :: c++ looping through a vector 
Cpp :: c++ function as paramter 
Cpp :: Visual studio code include path not working c++ 
Cpp :: how to concatenate two vectors in c++ 
Cpp :: vectors c++ 
Cpp :: convert all strings in vector to lowercase or uppercase c++ 
Cpp :: c++ constructor call superclass 
Cpp :: stack class implementation to file unix-style in c++ 
Cpp :: loop c++ 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =