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; }
iostreamyouriostreamincludestdio.h? Then specify the compyator, because not everyone can do it. And yes, I compile as C ++, by itself. At leaststdio.handstdlib.hmissing. - PinkTux