include stdio include stdlib include conio include windows include io

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <io.h>
#include <locale>
#define FNAME "myfile.bin"
void Reverse(char *filename)
{
FILE *f;
int buff, buff2;
bool canExit = false;
fpos_t dbg, len;
if (!(f = fopen(filename, "r+b")))
{
puts("error");
return;
}
len=filelength(fileno(f));
do
{
fread(&buff,sizeof(int),1,f);
fseek(f,sizeof(int),SEEK_CUR);
fread(&buff2,sizeof(int),1,f);
fseek(f,-sizeof(int),SEEK_CUR);
fwrite(&buff,sizeof(int),1,f);
fgetpos(f,&dbg);
if(dbg==len) canExit=true;
fseek(f,3*(-sizeof(int)),SEEK_CUR);
fwrite(&buff2,sizeof(int),1,f);
fflush(f);
} while(!canExit);
fclose(f);
}
void printFile(char *filename)
{
FILE *f;
int buff;
if (!(f = fopen(filename, "rb")))
{
puts("error");
return;
}
do
{
fread(&buff,sizeof(int),1,f);
if (!feof(f)) printf("%d",buff); else break;
} while(!feof(f));
puts("EOF");
fclose(f);
}
void inputFile(char *filename)
{
FILE *f;
int buff;
if (!(f = fopen(filename, "w+b")))
{
puts("error");
return;
}
do
{
printf("Введите число: ");
scanf("%d",&buff);
fflush(stdin);
fwrite(&buff,sizeof(int),1,f);
puts("Продолжить? (y/n)");
} while(getch()!='n');
fclose(f);
}
int main()
{
setlocale(LC_ALL,"rus");
do
{
puts("1 - print\n2 - Reverse\n3 - input\n0 - exit");
switch(getch())
{
case '1': printFile(FNAME); break;
case '2': Reverse(FNAME); break;
case '3': inputFile(FNAME); break;
case '0': return 0;
}
} while(1);
}