Reading and writing files
Read project files with std::ifstream; write temporary files under /tmp with std::ofstream.
Runs in Zerethon IDE · C++ (GCC 14)
Official C++ documentation ↗
main.cpp
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::ifstream in("data.csv");
if (!in) {
std::cerr << "cannot open data.csv\n";
return 1;
}
std::string line;
std::getline(in, line); // header
int rows = 0;
while (std::getline(in, line)) {
std::cout << line.substr(0, line.find(',')) << '\n';
++rows;
}
std::ofstream("/tmp/report.txt") << "rows=" << rows << '\n';
std::ifstream report("/tmp/report.txt");
std::getline(report, line);
std::cout << line << '\n';
}
data.csv
name,score
ada,90
alan,85
Output
ada alan rows=2
- Check the stream (
if (!in)) — opening a missing file does not throw. - Paths are relative to the project root. Project files are read-only; write to
/tmp.