// 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;
}
#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;
}
#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();
}
Code Example |
---|
Cpp :: c++ celsius to fahrenheit |
Cpp :: repeat character n times c++ |
Cpp :: how to iterate in string in c++ |
Cpp :: find all occurrences of a substring in a string c++ |
Cpp :: controlling in windows |
Cpp :: qimage transformed |
Cpp :: c++ fast |
Cpp :: how to create a pair of double quotes in c++ |
Cpp :: shuffle elements c++ |
Cpp :: cannot open include file unreal |
Cpp :: random in c++ |
Cpp :: input pdf latex |
Cpp :: factore of 20 in c+ |
Cpp :: how to output to console c++ |
Cpp :: c++ enum rand |
Cpp :: qstring get if empty |
Cpp :: c ++ program to search hashmap |
Cpp :: print vector |
Cpp :: c++ split string by space into vector |
Cpp :: how to open and print in a file in c++ |
Cpp :: save all output in log file c cpp |
Cpp :: findung the mode in c++ |
Cpp :: tic toc toe c++ |
Cpp :: c++ shared pointer |
Cpp :: opencv rgb to gray c++ |
Cpp :: how to open and read text files in c++ |
Cpp :: c++ for in |
Cpp :: c++ count number of element in vector |
Cpp :: c++ std::sort |
Cpp :: abs in c++ |