У меня есть код по использованию потокивих операций как его сделать с использованием файловых указателей?
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{;
vector<double> pos, neg;
int zeros = 0;
double x;
for (ifstream in("text1.txt"); in >> x; )
{
if (x > 0) pos.push_back(x);
else if (x < 0) neg.push_back(x);
else zeros++;
}
ofstream out("text_copy.txt");
for (double x : pos) out << x << "\n";
for (int i = 0; i < zeros; ++i) out << 0.0 << "\n";
for (double x : neg) out << x << "\n";
}
Ответы (1 шт):
Автор решения: Mikhailo
→ Ссылка
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
vector<double> pos, neg;
int zeros = 0;
double x;
FILE * in = fopen("text1.txt","r");
for (; fscanf(in,"%lf",&x)==1; )
{
if (x > 0) pos.push_back(x);
else if (x < 0) neg.push_back(x);
else zeros++;
}
fclose(in);
FILE * out = fopen("text_copy.txt","wt");
for (double x : pos) fprintf(out,"%lf\n",x);
for (int i = 0; i < zeros; ++i) fprintf(out,"%lf\n",0.0);
for (double x : neg) fprintf(out,"%lf\n",x);
fclose(out);
}
Так как-то...