Szerkesztő:LinguisticMystic/cpp/Files
Megjelenés
(Szerkesztő:LinguisticMystic/cpp/files szócikkből átirányítva)
✅ #include <fstream>
[szerkesztés]This header provides the C++ file stream classes:
std::ifstream– for reading from files.std::ofstream– for writing to files.std::fstream– for both reading and writing.
✅ Writing to a file
[szerkesztés]
Code:
[szerkesztés]#include <iostream>
#include <fstream>
int main() {
std::ofstream outputFile;
std::string filename = "SomeText.txt";
outputFile.open(filename);
outputFile << "Hi, I'm just a text you're trying to write to a file.";
outputFile.close();
return 0;
}
Explanation:
[szerkesztés]std::ofstream outputFile;– Declares an output file stream object.outputFile.open(filename);– Opens or creates the fileSomeText.txt.outputFile << ...– Writes text to the file.outputFile.close();– Closes the file. Always good practice.
📝 The file is created in the same directory where your program’s executable runs.
✅ Reading from a file
[szerkesztés]
Code:
[szerkesztés]#include <iostream>
#include <fstream>
int main() {
std::ifstream inputFile;
inputFile.open("SomeText.txt");
std::string line;
getline(inputFile, line);
inputFile.close();
std::cout << "Data from file: " << line << std::endl;
return 0;
}
Explanation:
[szerkesztés]std::ifstream– Used for reading.getline()– Reads one line from the file.inputFile.close()– Closes the file.- Output shows the content of the file on the console.
✅ File Modes (std::ios::...)
[szerkesztés]These are flags you can pass when opening files to control how they behave:
| Mode | Meaning |
|---|---|
std::ios::in |
Open for reading |
std::ios::out |
Open for writing (clears existing file) |
std::ios::app |
Append at end |
std::ios::ate |
Go to end immediately after opening |
std::ios::trunc |
Truncate existing file (default with out) |
std::ios::binary |
Binary mode (no newline translation) |
🛠 Combine modes using | (bitwise OR), e.g.:
std::ofstream file("file.txt", std::ios::app | std::ios::binary);
✅ Examples of file modes
[szerkesztés]
Write at the end of the file:
[szerkesztés]std::ofstream file("example.txt", std::ios::ate | std::ios::out);
Truncate file (erase content first):
[szerkesztés]std::ofstream file("example.txt", std::ios::trunc | std::ios::out);
✅ File manipulation with <cstdio>
[szerkesztés]
Rename (move) a file:
[szerkesztés]const char* oldFileName = "file.txt";
const char* newFileName = "../file.txt";
std::rename(oldFileName, newFileName);
Rename (just change the name):
[szerkesztés]std::rename("old.txt", "new.txt");
Delete a file:
[szerkesztés]std::remove("new.txt");
✅ Copy a file (binary-safe):
[szerkesztés]std::ifstream src("source.txt", std::ios::binary);
std::ofstream dst("dest.txt", std::ios::binary);
dst << src.rdbuf(); // Copy raw bytes
✅ Seek to a specific position in file
[szerkesztés]std::ifstream inputFile("data.txt");
inputFile.seekg(10); // Jump to 11th byte
char ch;
inputFile.get(ch); // Read that byte
✅ Working with Directories – <filesystem> (C++17+)
[szerkesztés]
Create a directory:
[szerkesztés]std::filesystem::create_directory("my_folder");
Rename or move a directory:
[szerkesztés]std::filesystem::rename("old_folder", "new_folder");
✅ Error handling
[szerkesztés]
File open check:
[szerkesztés]std::ifstream file("nonexistent.txt");
if (!file.is_open()) {
std::cerr << "Failed to open file!" << std::endl;
}
Detect end-of-file while reading:
[szerkesztés]std::ifstream inputFile("data.txt");
std::string line;
while (getline(inputFile, line)) {
if (!inputFile.eof()) {
// Process line
}
}
✅ Summary
[szerkesztés]| Task | Class/Function |
|---|---|
| Read a file | std::ifstream |
| Write to a file | std::ofstream |
| Read/write both | std::fstream |
| Check file state | is_open(), eof() |
| Manipulate files | std::rename(), std::remove() |
| Work with folders | <filesystem> (C++17+) |