Pascal/Turbo Pascal - Mi archivo .dat guarda basura

 
Vista:

Mi archivo .dat guarda basura

Publicado por Ru (1 intervención) el 04/10/2020 04:32:15
Hola buenas noches, estoy haciendo un programa donde tengo que crear un archivo y este mismo guarda basura, nose porque sera, si alguien me prodria ayudar se lo agradeceria. Les dejo el codigo:

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
program crearArchivo;
 
uses crt,dos,sysutils;
 
type
 registro = record
 nombre:String;
 edad:integer;
 end;
 
fichero= file of registro;
var
f:fichero;
op:integer;
 
 
 
 
procedure alta (var f:fichero);
var
r:registro;
begin
 
	reset(f);
	seek(f,filesize(f)+1);
	writeln('Introduzca nombre');
	readln(r.nombre);
	writeln('Introduzca edad);
	readln(r.edad);
	seek(f,filepos(f)-1);
	write(f,r);
	Close(f);
	end;
end;
procedure crearArchivo(var f:fichero);
begin
Assign(f,'datos.dat');
Rewrite(f);
Close(f);
end;
BEGIN
repeat
writeln('1. Crear archivo ');
writeln('2. Alta');
writeln('4. Salir');
writeln('');
readln(op);
case op of
1: begin
    crearArchivo(f);
    writeln('Archivo creado con exito');
end;
2: begin
   alta(f);
   end;
 until opcion=4;
END.
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

Mi archivo .dat guarda basura

Publicado por Carlos Maza (2 intervenciones) el 05/10/2020 03:08:20
Hola:

Algunas cosa que noté y corregí: (la numeración corresponde a tu programa original)
1) línea 28, introduzca edad: no cierras comilla simple
2) línea 36: el 'end' está de más
3) línea 63: usas variable 'opcion'. Debe ser 'op'
4) línea 58: 'begin' está de más

con esto ya corre.
Aparte,
1) pasé la variable r:registro para afuera, debajo de f:fichero
2) agregué opcion 3: listar para ver el contenido del archivo (muy sencillo, sin ningún refinamiento)

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
program crearArchivo;
 
uses crt,dos,sysutils;
 
type
registro = record
nombre:String;
edad:integer;
end;
 
fichero= file of registro;
var
f:fichero;
r:registro;
op:integer;
 
 
procedure listar;
begin
  Assign(f,'datos.dat');
  reset(f);
  while not eof(f) do
  begin
    read(f,r);
    writeln(r.nombre,'   ',r.edad);
  end;
  close(f);
  write('Presione cualquier tecla');readkey;
end;
 
procedure alta(var f:fichero);
begin
 reset(f);
  seek(f,filesize(f)+1);
  writeln('Introduzca nombre');
  readln(r.nombre);
  writeln('Introduzca edad');
  readln(r.edad);
  seek(f,filepos(f)-1);
  write(f,r);
  Close(f);
end;
 
procedure crearArchivo(var f:fichero);
begin
  Assign(f,'datos.dat');
  Rewrite(f);
  Close(f);
end;
 
BEGIN
repeat
    clrscr;
	writeln('1. Crear archivo ');
	writeln('2. Alta');
	writeln('3. Listar');
	writeln('4. Salir');
	writeln('');
	readln(op);
	case op of
		1: begin
			crearArchivo(f);
			writeln('Archivo creado con exito');
		   end;
		2: alta(f);
		3: listar;
	end;
until op=4;
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