I do everything the same, but for some reason I don’t want to rewrite it. What am I doing wrong? Here is the code:

#include <iostream> #include<time.h> using namespace std; void main() { FILE *f; srand( time( NULL ) ); const int size = 10; int a[size]; for( int i = 0; i < size; i++ ) { a[i] = rand() % 100 + 1; cout << a[i] << "\t"; } fopen_s( &f, "f.txt", "wb" ); fwrite( &a, sizeof( int ), size, f ); fclose( f ); cout << endl; fopen_s( &f, "f.txt", "rb" ); int a1[size]; fread( &a1, sizeof( int ), size, f ); fclose( f ); int ss = 0; for( int i = 0; i < size; i++ ) if( a1[i] % 2 == 0 ) { ss++; } int *tmp = new int[ss]; for( int i = 0, l = 0; i < size; i++ ) { if( a1[i] % 2 == 0 ) { tmp[l] = a1[i]; l++; } } fopen_s( &f, "g.txt", "wb" ); fwrite( &ss, sizeof( int ), 1, f ); fwrite( &tmp, sizeof( int ), sizeof( tmp ), f ); fclose( f ); fopen_s( &f, "g.txt", "rb" ); int *tmp2 = new int[ss]; fread( &tmp2, sizeof( int ), ss, f ); //Здесь не хочет переписыать fclose( f ); for( int i = 0; i < ss; i++ ) { //а здесь уже ошибка, так как оно почему-то удаляет память которую // я выделил для tmp2(в отладчике смотрел) cout << tmp2[i] << " "; } cout << endl; } 

Closed due to the fact that off-topic participants are Vladimir Martyanov , Denis , aleksandr barakin , Harry , Alex 22 Nov '16 at 10:16 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Denis, Alex
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • And in the course that your code does not even compile? - PinkTux
  • @PinkTux everything compiles for me - anonimys
  • @PinkTux maybe you are not in c ++ but did it run in si? - anonimys
  • one
    iostream your iostream include stdio.h ? Then specify the compyator, because not everyone can do it. And yes, I compile as C ++, by itself. At least stdio.h and stdlib.h missing. - PinkTux
  • @PinkTux did not help - anonimys

2 answers 2

In this sentence

 fwrite( &tmp, sizeof( int ), sizeof( tmp ), f ); 

You are trying to write the tmp pointer itself instead of a dynamically allocated array.

I think you meant

 fwrite( tmp, sizeof( int ), ss, f ); 

In this sentence

 fread( &tmp2, sizeof( int ), ss, f ); 

You want to rewrite the value of the tmp2 pointer tmp2 data from a file that is larger than the size of this pointer.

What you most likely need is the following.

 fread( &ss, sizeof( int ), 1, f ); fread( tmp2, sizeof( int ), ss, f ); 

Keep in mind that it would be better to write down, for example, the given sentence.

 fwrite( &a, sizeof( int ), size, f ); 

as

 fwrite( a, sizeof( int ), size, f ); 

    I don’t know how or why - but it worked (I used to do this before - it didn’t help)

      #include <iostream> #include<cstdio> #include<time.h> using namespace std; void main() { FILE *f; srand(time(NULL)); const int size = 10; int a[size]; for (int i = 0; i < size; i++) { a[i] = rand() % 100 + 1; cout << a[i] << "\t"; } fopen_s(&f, "f.txt", "wb"); fwrite(&a, sizeof(int), size, f); fclose(f); cout << endl; fopen_s(&f, "f.txt", "rb"); int a1[size]; fread(&a1, sizeof(int), size, f); fclose(f); int ss = 0; for (int i = 0; i < size; i++) if (a1[i] % 2 == 0) { ss++; } int *tmp = new int[ss]; for (int i = 0, l=0; i < size; i++) { if (a1[i] % 2 == 0) { tmp[l] = a1[i]; l++; } } fopen_s(&f, "g.txt", "wb"); fwrite(&tmp, sizeof(int), ss, f); //удалил те две строчки и прописал эту fclose(f); fopen_s(&f, "g.txt", "rb"); int *tmp2 = new int[ss]; fread(&tmp2, sizeof(int), ss, f); fclose(f); for (int i = 0; i < ss; i++) cout << tmp2[i] << " "; cout << endl; 

    }