#include#include using namespace std; int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( getline (myfile,line) ) { cout << line << '\n'; } myfile.close(); } else cout << "Unable to open file"; return 0; }
#include#include using namespace std; int main () { ofstream myfile; myfile.open ("example.txt"); myfile << "Writing this to a file.\n"; myfile.close(); return 0; }
#includeThis code gets information about the file "example.txt", such as its size and the time it was last modified. The C++ File library is part of the C++ Standard Library.#include #include #include using namespace std; int main () { struct stat file_info; stat("example.txt", &file_info); cout << "Size of file is " << file_info.st_size << " bytes\n"; cout << "Last modified on " << ctime(&file_info.st_mtime); return 0; }