-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFichBits.cpp
More file actions
85 lines (74 loc) · 1.58 KB
/
Copy pathFichBits.cpp
File metadata and controls
85 lines (74 loc) · 1.58 KB
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
#include <stdio.h>
#include "FichBits.h"
static TFichBits FichES;
//
// Rutinas para escribir en fichero
//
int InicializaEscritura(char *nombre)
{
if ((FichES.fich=fopen(nombre,"wb"))==NULL) return 1;
FichES.salida_byte = 0x00;
FichES.pos=8;
return 0;
}
// escribe una palabra de nbits en el fichero
void EscribePalabra(int nbits, int palabra)
{
while (nbits>FichES.pos)
{
nbits-=FichES.pos;
FichES.salida_byte |= (palabra>>nbits);
putc((int)FichES.salida_byte,FichES.fich);
FichES.pos=8;
FichES.salida_byte=0;
}
FichES.pos-=nbits;
FichES.salida_byte |= (palabra<<FichES.pos);
}
// escribe un bit en el fichero
void EscribeBit(int bit)
{
EscribePalabra(1,bit);
}
void FinalizaEscritura()
{
if (FichES.pos!=8) putc((int)FichES.salida_byte,FichES.fich);
fclose(FichES.fich);
}
//
// Rutinas para leer de fichero
//
int InicializaLectura(char *nombre)
{
if ((FichES.fich=fopen(nombre,"rb"))==NULL) return 1;
FichES.quedan=FichES.entrada_byte = 0;
FichES.entrada_byte=getc(FichES.fich);
FichES.quedan=8;
return 0;
}
// lee una palabra de nbits del fichero
int LeePalabra(int nbits)
{
int f,palabra=0;
for (f=0;f<nbits;f++)
palabra=(palabra<<1)|LeeBit();
return palabra;
}
// lee un bit del fichero
int LeeBit()
{
int bit;
if (FichES.quedan==0)
{
FichES.entrada_byte=getc(FichES.fich);
FichES.quedan=8;
}
bit=(FichES.entrada_byte&0x80)>>7;
FichES.entrada_byte<<=1;
FichES.quedan--;
return bit;
}
void FinalizaLectura()
{
fclose(FichES.fich);
}