Answer the question
In order to leave comments, you need to log in
Reading a binary file without using an array?
Is it possible to read a binary file without an array? I wrote a program that creates two files, adds arbitrary numbers to one, and to the other according to the desired criterion from the first. The program does not work quite correctly: it takes the last value of the first file and compares it according to the necessary criteria, instead of checking the entire area. There is a solution: use an array and then work with it, but alas, the conditions do not allow using it.
Screenshot:
Here is the code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
srand((unsigned int) time(0));
double numeric;
int n;
cout << "Введите количество чисел:";
cin >> n;
ofstream f("f.bin", ios::binary|ios::out);
for(int i=0; i<n; i++)
{
numeric = (rand()%10);
f.write((char *)&numeric, sizeof(numeric));
cout << numeric << " ";
}
cout << endl;
f.close();
ofstream g("g.bin", ios::binary|ios::out);
ofstream i_f("f.bin", ios::binary|ios::in);
for(int i=0; i<n; i++)
{
if(numeric < 10)
{
g.write((char *)&numeric, sizeof(numeric));
cout << numeric << " ";
}
}
cout << endl;
f.close();
g.close();
system("pause");
return 0;
}
Answer the question
In order to leave comments, you need to log in
you need to take numbers from a file, not one read at the same time.
would comment on the code themselves and understand what kind of ... Good luck.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question