太久不用文件流,有些生疏了。
和IO流同为流操作,二者有许多共同之处。比如好用的unget(),在许多情况下为了简化逻辑这个方法是必不可少的。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream readtxt("C://Users/Cruciata/Desktop/txt/Feature selection results2.txt", ios::in);
if (!readtxt) {
cout << "error read" << endl;
return 0;
}
ofstream wtxt("C://Users/Cruciata/Desktop/txt/out.txt", ios::out);
if (!wtxt) {
readtxt.close();
cout << "error write" << endl;
return 0;
}
double x;
double y=0; //准确度
double y0;
double p[100];
bool flag57 = 0;
char a;
int i = 0;
while (readtxt >> a)
{
if ((a != '-') && (a != '>'))
{
readtxt.unget(); //返还流
readtxt >> x; //cout << x << " ";
if (x == 57) flag57 = 1;
y0 = y; //推迟一个循环输出
if (x < 1)
{
y = x;
}
else p[i++] = x;
}
else if (a == '>')
{
if (flag57 == 1&&y0>0.8)
{
wtxt << y0 << " ";
for (int j = 0; j < i; j++)
{
wtxt << p[j] << " ";
}
wtxt << endl;
}
i = 0;
flag57 = 0;
}
}
wtxt.close();
readtxt.close();
return 0;
}
}