Answer the question
In order to leave comments, you need to log in
How to process a binary file in c++?
Hello! The condition of the problem is as follows: given a binary file of integers, write all prime numbers to another file, swap the 2 smallest numbers in the second file, without using arrays. In fact, the problem is in a small piece of code that puts the cursor in the file in the right place:
fseek (f, sizeof(int)+min11*sizeof (int), SEEK_SET);
fwrite (&min2, sizeof(int), 1, f);
fseek(f, sizeof(int)+min22*sizeof (int), SEEK_SET);
fwrite (&min1, sizeof (int), 1, f);
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;
int create_file ()
{
FILE *f; int n, i, *a;
cout<<"n="; cin>>n;
a=new int [n];
for (i=0; i<n; i++) {
cout<<"a["<<i<<"]="; cin>>a[i]; cout<<"\n"; }
f=fopen("abc.dat", "wb");
fwrite(&n, sizeof(int), 1, f);
fwrite(a, sizeof(int), n, f);
fclose(f);
cout<<"\nCOMPLETED";
return 0;
}
bool Prostoe (int n)
{
int k; bool Pr;
for (k=2, Pr=true; k <= n/2; k++)
if (n%k==0)
{Pr=false; break;}
return Pr;
}
int pr_file()
{
FILE *f;
int i, n, *a;
f=fopen ("abc.dat", "rb");
fread(&n, sizeof (int), 1, f);
cout<<n<<endl;
a=new int [n];
fread (a, sizeof (int), n, f);
fclose(f);
f=fopen ("abc1.dat", "wb");
for (i=0; i<n; i++)
if (Prostoe(a[i])) {
fwrite(&a[i], sizeof (int), 1, f);
cout<<"a["<<i<<"]="<<a[i]<<endl;}
fclose (f);
cout<<"\nCOMPLETED\n";
return 0;
}
int delete_file ()
{
FILE *f; int a,min2,min1, pr=0,min11, min22;
f=fopen("abc1.dat", "rb");
for(pr=0;!feof(f); pr++) {
fread (&a, sizeof(int), 1, f);
if (pr==0) {min1=a;min11=pr;}
else if (pr==1){ if (a<min1) {min2=min1;min22=min11;min1=a; min11=pr;}
else {min2=a; min22=pr;}}
else if (a<min1) {min2=min1;min22=min11; min1=a; min11=pr;}
else if (a<min2) {min2=a;min22=pr;}}
fclose(f);
f=fopen("abc1.dat", "ab+");
cout<<"min1="<<min1<<endl;
cout<<"min2="<<min2<<endl;
cout<<"min11="<<min11<<endl;
cout<<"min22="<<min22<<endl;
fseek (f, sizeof(int)+min11*sizeof (int), SEEK_SET);
fwrite (&min2, sizeof(int), 1, f);
fseek(f, sizeof(int)+min22*sizeof (int), SEEK_SET);
fwrite (&min1, sizeof (int), 1, f);
fclose (f);
cout<<"\nCOMPLETED\n";
return 0;
}
int read_file()
{
FILE *f;
int a;
f=fopen("abc1.dat", "rb");
while (!feof(f)) {
fread (&a, sizeof(int), 1, f);
cout<<"a="<<a<<"\n"; }
fclose(f);
cout<<"\nCOMPLETED\n";
return 0;
}
int main(int argc, char **argv)
{
int i;
cout<<"1. Create file\n2. Search simple numbers\n3. Delete 2 min numbers\n4. Read file\n0. Esc\n";
cin>>i;
for (;i!=0; cin>>i) {
if (i==1)
create_file();
if (i==2)
pr_file();
if (i==3)
delete_file();
if (i==4)
read_file(); }
return 0;
}
Answer the question
In order to leave comments, you need to log in
why in fseek sizeof(int)+ at all?
and if the minimum at offset 0 will you still write at offset 4?
The problem is that in the "a +" mode, writing always occurs at the end of the file.
Quote from man fopen:
Related question on SO:
stackoverflow.com/questions/3645123/opening-a-file...
The standard C language does not provide the ability to write to an arbitrary location in a file. You need to change the algorithm, or use some more low-level and platform-specific tools.
Start by saying that checking for a prime number is enough to drive the cycle to (the root of n) +1 :-) otherwise the teacher will not understand you or where you need to
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question