彩票網(wǎng)站建設(shè) 極云互聯(lián)網(wǎng)營銷工具有哪些
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
/*
輸入任意多個整數(shù), 把這些數(shù)據(jù)保存到文件data.txt中.
如果在輸入的過程中, 輸入錯誤, 則提示用戶重新輸入.
指導(dǎo)用戶輸入結(jié)束(按ctrl + z)
[每行最多保存10個整數(shù)]
*/
int main()
{
?? ?int num;
?? ?int flag = 0;
?? ?ofstream ofs;
?? ?ofs.open("data.txt");?? ?//?
?? ?if (!ofs.is_open()) {
?? ??? ?cout << "文件打開失敗" << endl;
?? ??? ?exit(1);
?? ?}
?? ?while (1) {
?? ??? ?cout << "請輸入整數(shù):";
?? ??? ?cin >> num;
?? ??? ?if (cin.eof()) {
?? ??? ??? ?cout << "結(jié)束輸入" << endl;
?? ??? ??? ?break;
?? ??? ?}
?? ??? ?while(cin.fail()) {
?? ??? ??? ?cin.clear();?? ?//清空錯誤標志
?? ??? ??? ?//cin.sync();?? ??? ?//VS中無效
?? ??? ??? ?//cin.ignore(10, '1');
?? ??? ??? ?cin.ignore(numeric_limits<streamsize>::max(), '\n');
?? ??? ??? ?//流的最大,緩沖區(qū)中所有數(shù)據(jù) ? ? ?扔掉緩沖區(qū)所有數(shù)據(jù)包括\n
?? ??? ??? ?cout << "輸入錯誤,請重新輸入:";
?? ??? ??? ?cin >> num;
?? ??? ?}
?? ??? ?ofs << num << '\t';
?? ??? ?if (++flag % 10 == 0) {
?? ??? ??? ?ofs << endl;
?? ??? ?}
?? ?}
?? ?ofs.close();
?? ?system("pause");
?? ?return 0;
};
};