从键盘输入一个字符串、一个整数和一个实数,将其写入文件f1.txt中,再用读字符方式从此文本文件中逐个读出并显示在屏幕上
从键盘输入一个字符串、一个整数和一个实数,将其写入文件f1.txt中,再用读字符方式从此文本文件中逐个读出并显示在屏幕上程序代码:
#include<iostream> #include<fstream> using namespace std; int main(void) { char buf[1024]; int value; double number; ofstream ofs; ofs.open("test.txt"); if(ofs.is_open()){ cout << "opening file success!" << endl; } cout << "Enter your string :"; cin.getline(buf, 100); ofs << buf << endl; cout << "Enter your int :"; cin >> value; ofs << value << endl; cout << "Enter your double :"; cin >> number; ofs << number << endl; ifstream ifs; ifs.open("test.txt", ios::in); if(ifs.is_open()){ cout << "opening file success!" << endl; } cout << "Reading the file:" << endl; ifs >> buf; cout << buf << endl; ifs >> value; cout << value << endl; ifs >> number; cout << number << endl; return 0; }
[此贴子已经被作者于2020-5-18 08:14编辑过]