/// <summary> /// 打开DAT 文件 /// </summary> void operatefile() { char data[100]; const char* fname = "afile.dat"; // 打开文件. ofstream outfile; outfile.open(fname,ios::in); if (!outfile) { cout << "文件不存在!" << endl; ofstream fout(fname); //创建文件 } cout << "写文件" << endl; cout << "输入姓名: "; cin.getline(data, 100); // 写入. outfile << data << endl; cout << "输入年龄: "; cin >> data; cin.ignore(); // 写入. outfile << data << endl; // 关闭文件. outfile.close(); // 打开文件. ifstream infile; infile.open("afile.dat"); cout << "读文件内容:" << endl; infile >> data; // cout << data << endl; // infile >> data; cout << data << endl; // 关闭文件 infile.close(); }
标签:char,cout,read,dat,outfile,file,cpp,data From: https://www.cnblogs.com/geovindu/p/17739804.html