Pascal/Turbo Pascal - Modificar archivo (offsets)

 
Vista:

Modificar archivo (offsets)

Publicado por Zeta (2 intervenciones) el 19/08/2016 19:10:01
Saludos.

Realmente deseo aprender a modificar texto.

Abro el blog de notas, y escribo: searching

Luego guardo el archivo en formato txt. (Si abro este archivo con un editor hexagesimal, veré cual es el offset + los bytes de la palabra searching)

Supongamos que deseo cambiar la palabra searching por: Busqueda.


Mi deseo es crear un programa en consola que modifique el texto del archivo. Pero tomando en cuenta el offset y los bytes. Para posteriormente guardar los cambios en el mismo archivo.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Modificar archivo (offsets)

Publicado por ramon (2158 intervenciones) el 23/08/2016 12:26:57
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
{Mira esto te puede servir como ejemplo}
 
program offsets;
  uses
     crt;
   type
     dato = array[0..32000] of char;
     const
     mitexto : array[1..22] of string[78] = (
'Un circuito electrico es un sistema interconectado de componentes como',
'resistores, capacitores, inductores, fuentes de voltaje, etc.',
'El comportamiento electrico de estos componentes se describe por medio',
'de algunas leyes basicas experimentales.',
'Dichas leyes y los principios, conceptos, relaciones matematicas y',
'metodos de analisis que se han desarrollado a partir de ellos, son',
'conocidos como la teoria del circuito.',
'La mayor parte de la teoria del circuito tiene que ver con la resolucion',
'de problemas y con el analisis numerico.',
'Cuando se analiza un problema o se disea¤a un circuito, por ejemplo, se',
'necesita calcular valores para voltaje, corriente y potencia.',
'Ademas del valor numurico, la respuesta debe incluir la unidad.',
'El sistema de unidades que se utiliza para este proposito es el sistema',
'SI (Sistema Internacional), un sistema unificado de medicion metrica que',
'abarca no solo las unidades de longitud, masa y tiempo, los conocidos MKS',
'(metros, kilogramos y segundos), sino tambien unidades de cantidades',
'electricas y magneticas.',
'Sin embargo, es frecuente que las unidades del SI produzcan numeros muy',
'grandes o muy peque¤os para usarse de manera practica.',
'Para manejar esto se ha desarrollado la notacion de ingenieria y un',
'conjunto de prefijos estandar.',
'Su uso en representacion y computacion se describe e ilustra.');
 
 nombtexto : string = 'Architem.txt';
 
  var
    texto : string;
    dat : dato;
    te, letra : char;
    tpf, f : text;
    frase : string;
    dj, linea, inicio, fonal : integer;
    long, t : longint;
    prim : boolean;
 
   procedure creaarchivotexto;
   var
      co : integer;
   begin
       assign(tpf, nombtexto);
    {$I-} reset(tpf); {$I+}
    if ioresult <> 0 then
    begin
      rewrite(tpf);
      writeln(tpf,mitexto[1]);
      close(tpf);
      Append(tpf);
      for co := 2 to 22 do
      begin
         writeln(tpf,mitexto[co]);
      end;
       close(tpf);
    end
  else
     close(tpf);
   end;
 
   function anulamitexto : boolean;
   begin
       anulamitexto := false;
       assign(tpf, nombtexto);
    {$I-} reset(tpf); {$I+}
    if ioresult = 0 then
    begin
       close(tpf);
       erase(tpf);
       anulamitexto := true;
    end
  else
      anulamitexto := false;
    end;
 
   procedure leearchivo(nombre : string);
   begin
      assign(f, nombre);
    {$I-} reset(f); {$I+}
    if ioresult <> 0 then
    begin
       writeln('   Error De Archivo O No Existe Pulse Una Tecla');
       readkey;
   end
 else
     begin
        t := 0;
        long := 0;
        while not eof(f) do
        begin
           read(f,letra);
           dat[t] :=  letra;
           if letra = #13 then
           writeln
        else
           write(dat[t]);
           t := t + 1;
           if t > 32000 then
           t := 32000;
        end;
        long := (t - 1);
        close(f);
     end;
   end;
 
  procedure buscarfrase(fra : string);
  var
    hh : longint;
    tem, franu, tempo : string;
    p, t, y, lar : integer;
    encontrada : boolean;
    espa : string[20];
  begin
     encontrada := false;
     tempo := ' ';
     for y := 1 to length(fra) do
     fra[y] := upcase(fra[y]);
     lar := 1;
     for hh := 0 to long do
     begin
        if dat[hh] in[' ','.',',','(','{',':',';'] then
        begin
           if fra = tempo then
           begin
           hh := hh - (lar - 1);
           encontrada := true;
           break;
           end
        else
           begin
              lar := 1;
              tempo := ' ';
           end;
         end
        else
           begin
             tempo[lar] := upcase(dat[hh]);
             tempo[0] := chr(lar);
             lar := lar + 1;
          end;
     end;
     if encontrada = true then
     begin
        clrscr;
        writeln;
        write('    ');
        for t := hh to hh + (length(tempo) - 1) do
        write(dat[t]);
        writeln;
        write('Entre Frase Nueva : ');
        readln(franu);
        if length(franu) > length(tempo) then
        writeln('La Longitud Deve Ser La Misma Que La Frase Cambiada')
     else
        begin
        tem := ' ';
        tem := copy(franu,1,length(franu));
        if length(tempo) > length(franu) then
        begin
           p := 1;
           for y := length(franu) to length(tempo) do
           begin
           espa[p] := ' ';
           espa[0] := chr(p);
           p := p + 1;
           end;
           insert(espa,tem,length(tem) + 1);
         end;
        y := 1;
        for t := hh to hh + (length(tem) - 1) do
        begin
        dat[t] := tem[y];
        y := y + 1;
        end;
         clrscr;
         for hh := 0 to long do
         begin
            if dat[hh] <> #13 then
            write(dat[hh])
          else
             writeln;
          end;
        end;
     end;
  end;
 
 
 
 
    begin
       creaarchivotexto;
       clrscr;
       leearchivo(nombtexto);
       writeln;
       writeln('   Entre Frase A Modificar');
       write('   Frase : ');
       readln(frase);
       buscarfrase(frase);
       writeln;
       writeln('  Desea Guardar Los Cambios [S/N]');
       repeat
           te := upcase(readkey);
       until te in ['S','N'];
       if te = 'S' then
       begin
          assign(f, nombtexto);
       {$I-} reset(f); {$I+}
       if ioresult = 0 then
       begin
          close(f);
          erase(f);
       end;
         prim := false;
         dj := 1;
         for t := 0 to long do
         begin
            if dat[t] <> #13 then
            begin
            texto[dj] := dat[t];
            texto[0] := chr(dj);
            dj := dj + 1;
         end
       else
          begin
             if prim = false then
             begin
             rewrite(f);
             prim := true;
             writeln(f,texto);
             close(f);
             end
          else
             begin
               append(f);
               writeln(f,texto);
               close(f);
             end;
             dj := 1;
             texto := ' ';
          end;
       end;
     end;
       writeln;
       writeln('   Pulse Una Tecla');
       readkey;
       writeln(anulamitexto);
    end.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar