#include <iostream> #include <fstream> using namespace std; int main(void) { ofstream ofs("file.txt");//类似fprintf,写文件 ofs << 123 << ' ' << 4.56 << ' ' << "HelloWorld" << endl; ofs.close(); ifstream ifs("file.txt");//类似fscanf,读文件 int i; double d; char s[20] = {0}; ifs >> i >> d >> s; cout << "i=" << i << ",d=" << d << ",s=" << s << endl; ifs.close(); return 0; }
#include <iostream> #include <fstream> using namespace std; int main(void) { ofstream ofs("last.txt"); char wbuf[] = "五一假期快乐!"; ofs.write(wbuf,sizeof(wbuf)); ofs.close(); ifstream ifs("last.txt"); char rbuf[100] = {0}; ifs.read(rbuf,sizeof(rbuf)); cout << "读到数据:" << rbuf << endl; ifs.close(); return 0; }