Skip to content
Sign upLog in
← Back to Community

C++ Fstream File Handling ( Tutorial )

Profile icon
JaydenSavarinat
#include <iostream> #include <fstream> // Inlcude fstream library to handle files #include <cstring> /************************************************ - - - - - KEY - - - - - ofstream - Creates and writes to files ifstream - Reads from files fstream - creates, reads, and writes to files ************************************************/ int main() { std::string FileText; // Creates a file named "File.txt" and opens it std::ofstream MyFile("File.txt"); // Writes "Hello File!" to "File.txt" MyFile << "Hello file! \n"; MyFile << "Hello file! \n"; MyFile << "Hello file! \n"; // Closes "File.txt" MyFile.close(); // Reads text from "File.txt" std::ifstream Read("File.txt"); while (getline(Read, FileText)) { // Outputs all text from "File.txt" std::cout << FileText << std::endl; } }
Voters
Profile icon
WILLIAMBAEWER
Profile icon
CraftPaul14
Profile icon
programmeruser
Profile icon
TsunamiOrSumth
Profile icon
yzhacker
Profile icon
JaydenSavarinat
Comments
hotnewtop
Profile icon
yzhacker

Nice Work Bro

Profile icon
JaydenSavarinat

@qerewrad
Thank you! Also, feel free to join my developer group. Click below to join. https://repl.it/teams/join/qqepyfbajpqyfnooryesrpdcdfpifnkp-ReplitDevTeam