La Web del Programador: Comunidad de Programadores
 
    Pregunta:  59883 - PROBLEMA CON LOS TIPOS DE DATO
Autor:  abe
Hola!
Quisiera saber cómo puedo leer un archivo tipo texto y la información convertirla a tipo integer..es decir

creo un archivo tipo texto, y cuando lo leo desde pascal, comienzo a leer cada caracter del archivo, pero resulta que ese caracter es un número, pero pascal me lo está leyendo como un string,

const
ruta='e:informacion.txt'

var
archivo:text;
numero1,numero2: string;

begin
assign(archivo,ruta);
reset(archivo);
while not eoln(archivo) do
begin
read(archivo,numero1,numero2);
suma:=numero1+numero2;
write(suma);
end;
readln(archivo);
writeln;
close(archivo);

He intentado declarar a numero1 y numero2 como integer, pero me sale el error de que lo que pascal leyó del archivo fue un caracter no un número.

  Respuesta:  J.Ant. Bernal
Prueba con la función StrToInt(): (de la ayuda de Virtual Pascal 2.1 para OS/2, creo recordar que en Turbo-Borland Pascal también está,)

function StrToInt(const S: string): Integer;

StrToInt converts the given string to an integer value. If the string doesn't contain a valid value, an EConvertError exception is raised.

See also StrToIntDef, Val.

Examples:
StrToInt( ' 100' ) = 100
StrToInt( ' 100' ) = 100
StrToInt( '100 ' ) -> Exception is raised
StrToInt( '-10' ) = -10