#include <iostream>
using namespace std;
void View( FILE * ,char * ) ;
void NameFile( char * , char * ) ;
void Create( FILE * ) ;
int Menu( ) ;
int main( )
{
FILE * a, * b;
int x, y, n;
int ptr1, ptr2, k;
char nameA[ 20 ] , nameB[ 20 ] ;
setlocale( LC_ALL, "" ) ;
NameFile( nameA, "Введите имя исходного файла:" ) ;
while ( true )
{
switch ( Menu( ) )
{
case 1 :
if ( ! ( a = fopen ( nameA, "wb" ) ) )
{
cout << "\n Ошибка создания файла " << nameA << ".\n " ;
return 1 ;
}
Create( a) ;
fclose ( a) ;
break ;
case 2 :
if ( ! ( a = fopen ( nameA, "rb+" ) ) )
{
cout << "\n Ошибка открытия файла " << nameA << ".\n " ;
return 1 ;
}
View( a, "\n Данные файла А:\n " ) ;
fclose ( a) ;
break ;
case 3 :
if ( ! ( a = fopen ( nameA, "rb" ) ) )
{
cout << "\n Ошибка открытия файла " << nameA << ".\n " ;
return 1 ;
}
NameFile( nameB, "Введите имя результирующего файла:" ) ;
if ( ! ( b = fopen ( nameB, "wb" ) ) )
{
cout << "\n Ошибка создания файла " << nameB << ".\n " ;
return 1 ;
}
fread ( & x, sizeof ( x) , 1 , a) ;
fwrite ( & x, sizeof ( x) , 1 , b) ;
ptr1 = ptr2 = k= 1 ;
cout << "\n x=" << x << " ptr1=" << ptr1 << " ptr2=" << ptr2 << " k=" << k;
while ( true )
{
while ( ( n= fread ( & y, sizeof ( int ) , 1 , a) ) == 1 && x* y > 0 )
{
k++ ;
cout << "\n y=" << y << " ptr1=" << ptr1 << " ptr2=" << ptr2 << " k=" << k;
}
if ( n ! = 1 )
break ;
fwrite ( & y, sizeof ( int ) , 1 , b) ;
x = y;
ptr2 = k+ 1 ;
k++ ;
cout << "\n x=" << x << " ptr1=" << ptr1 << " ptr2=" << ptr2 << " k=" << k;
fseek ( a, ptr1* sizeof ( int ) , 0 ) ;
fread ( & x, sizeof ( int ) , 1 , a) ;
fwrite ( & x, sizeof ( int ) , 1 , b) ;
ptr1++ ;
cout << "\n x=" << x << " ptr1=" << ptr1 << " ptr2=" << ptr2 << " k=" << k;
fread ( & y, sizeof ( int ) , 1 , a) ;
if ( x* y< 0 )
fseek ( a, ptr2* sizeof ( int ) , 0 ) ;
cout << "\n y=" << y << " ptr1=" << ptr1 << " ptr2=" << ptr2 << " k=" << k;
system ( "pause" ) ;
}
fclose ( a) ;
fclose ( b) ;
break ;
case 4 :
if ( ! ( b = fopen ( nameA, "rb" ) ) )
{
cout << "\n Ошибка создания файла " << nameB << ".\n " ;
return 1 ;
}
View( b, "\n Данные файла B:\n " ) ;
fclose ( b) ;
break ;
case 0 :
system ( "pause" ) ;
return 0 ;
}
}
}
void NameFile( char * name, char * s)
{
puts ( s) ;
fflush ( stdin ) ;
gets ( name) ;
}
void Create( FILE * f)
{
int x;
while ( true )
{
cout << "\n Введите число (0 - конец ввода):\n " ;
cin >> x;
if ( ! x) break ;
fwrite ( & x, sizeof ( int ) , 1 , f) ;
}
}
void View( FILE * f, char * s)
{
int x;
cout << s;
while ( fread ( & x, sizeof ( int ) , 1 , f) == 1 )
cout << x << " " ;
cout << endl;
}
int Menu( )
{
int i;
do
{
cout << "Выберите режим работы\n " ;
cout << "1 - создание файла;\n " ;
cout << "2 - просмотр заданного файла;\n " ;
cout << "3 - создание файла В;\n " ;
cout << "4 - просмотр файла В.\n " ;
cout << "0 - выход\n " ;
cin >> i;
} while ( i> 4 || i< 0 ) ;
return i;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKdm9pZCBWaWV3KEZJTEUqLGNoYXIqKTsKdm9pZCBOYW1lRmlsZShjaGFyKiwgY2hhciopOwp2b2lkIENyZWF0ZShGSUxFKik7CmludCBNZW51KCk7CgppbnQgbWFpbigpCnsKCUZJTEUgKmEsICpiOwoJaW50IHgsIHksIG47CglpbnQgcHRyMSwgcHRyMiwgazsKCWNoYXIgbmFtZUFbMjBdLCBuYW1lQlsyMF07CgoJc2V0bG9jYWxlKEx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