Problema ao Ler um arquivo
Olá estou fazendo um pequeno em Pascal para criar e ler um arquivo, a parte da leitura do arquivo não esta funcionando, alguem poderia me ajudar por favor?
program Ed_Wilton_lista1;
uses crt;
{declaração de variaveis}
var
opc, i : integer;
arq: text;
resparq, resp, pos: char;
dir, wnome, wende: string;
nome, ende : array[0..10] of string;
{Inicio Procedures}
procedure error;
begin
writeln('Opcao Invalida! Tente Novamente.');
end;
procedure menu;
begin
writeln('ESCOLHA A OPCAO DESEJA NO MENU ABAIXO!');
writeln('**************************************');
writeln('* 1 - INSERIR *');
writeln('* 2 - REMOVER *');
writeln('* 3 - LISTAR *');
writeln('* 4 - PESQUISAR *');
writeln('* 5 - SAIR *');
writeln('**************************************');
write('>>');
end;
procedure GravaArquivo;
var textarq : string;
begin
assign(arq,dir);
if (resparq = 'L') then
Erase(arq);
Rewrite(arq);
for i:=0 to 9 do
begin
textarq := nome[i] + ',' + ende[i];
if textarq = ',' then
break;
write(arq, textarq);
writeln(arq, '');
end;
Close(arq);
Writeln('DADOS GRAVADO NO ARQUIVO COM SUCESSO');
end;
procedure LerArquivo;
begin
assign(arq,dir);
reset(arq);
i := 0;
while (not eof(arq)) do
begin
read(arq,nome[i]);
read(arq,ende[i]);
i := i + 1;
end;
Close(arq);
end;
procedure inserir;
begin
resp := 'S';
i := 0;
while (resp = 'S') do
begin
write('Digite um nome: ');
readln(wnome);
write('Digite o endereco: ');
readln(wende);
Repeat
write('Deseja inserir os dados no <i>nicio ou no <f>im do arquivo: ');
readln(pos);
pos := Chr(Ord(pos)-32);
if ((pos <> 'I') and (pos <> 'F')) then
error;
until(pos = 'I') or (pos = 'F');
case pos of
'I' : begin
writeln('Dados Inseridos no Inicio do Arquivo');
if (nome[0] <> '') then
begin
for i:=0 to 9 do
begin
if i <> 9 then
begin
nome[9-i] := nome[8-i];
ende[9-i] := ende[8-i];
end;
end;
end;
nome[0] := wnome;
ende[0] := wende;
end;
'F' : begin
writeln('Dados Inseridos no Fim do Arquivo');
if (nome[9] <> '') then
begin
for i:=0 to 8 do
begin
nome[i] := nome[1+i];
ende[i] := ende[1+i];
end;
end;
nome[9] := wnome;
ende[9] := wende;
end;
end;
repeat
write('Deseja cadastrar novamente <S>im ou <N>ao: ');
readln(resp);
resp := Chr(Ord(resp)-32);
if ((resp <> 'S') and (resp <> 'N')) then
error;
until(resp = 'S') or (resp = 'N');
if (resp = 'S') then
i := i + 1;
end;
end;
procedure listar;
begin
for i:=0 to 9 do
begin
write(i);
writeln(' ' + nome[i] + '-' + ende[i]);
end;
writeln('Fim da lista');
readkey;
end;
{sistema principal}
begin
dir := 'C:\Pinga\Ed_Wilton_Lista1.txt';
Repeat
write('Deseja <C>riar ou <L>er um arquivo: ');
readln(resparq);
resparq := Chr(Ord(resparq)-32);
if ((resparq <> 'C') and (resparq <> 'L')) then
error;
until(resparq = 'C') or (resparq = 'L');
if resparq = 'L' then
LerArquivo;
textbackground(green);
textcolor(yellow);
while (opc < 5) do
begin
clrscr;
opc := 0;
menu;
readln(opc);
case opc of
1 : begin
writeln('Inserir Dados');
inserir;
end;
2 : writeln('Remover');
3 : begin
writeln('Listar Dados');
listar;
end;
4 : writeln('Pesquisar');
5 : begin
GravaArquivo;
writeln('SISTEMA ENCERRADO.OBRIGADO!');
end;
end;
if (opc > 5) then
begin
Error;
readkey;
end;
end;
readkey;
end.
O codigo é esse quem puder ajudar eu agradeço muito.
obrigado
Deus Abençoe a todos
program Ed_Wilton_lista1;
uses crt;
{declaração de variaveis}
var
opc, i : integer;
arq: text;
resparq, resp, pos: char;
dir, wnome, wende: string;
nome, ende : array[0..10] of string;
{Inicio Procedures}
procedure error;
begin
writeln('Opcao Invalida! Tente Novamente.');
end;
procedure menu;
begin
writeln('ESCOLHA A OPCAO DESEJA NO MENU ABAIXO!');
writeln('**************************************');
writeln('* 1 - INSERIR *');
writeln('* 2 - REMOVER *');
writeln('* 3 - LISTAR *');
writeln('* 4 - PESQUISAR *');
writeln('* 5 - SAIR *');
writeln('**************************************');
write('>>');
end;
procedure GravaArquivo;
var textarq : string;
begin
assign(arq,dir);
if (resparq = 'L') then
Erase(arq);
Rewrite(arq);
for i:=0 to 9 do
begin
textarq := nome[i] + ',' + ende[i];
if textarq = ',' then
break;
write(arq, textarq);
writeln(arq, '');
end;
Close(arq);
Writeln('DADOS GRAVADO NO ARQUIVO COM SUCESSO');
end;
procedure LerArquivo;
begin
assign(arq,dir);
reset(arq);
i := 0;
while (not eof(arq)) do
begin
read(arq,nome[i]);
read(arq,ende[i]);
i := i + 1;
end;
Close(arq);
end;
procedure inserir;
begin
resp := 'S';
i := 0;
while (resp = 'S') do
begin
write('Digite um nome: ');
readln(wnome);
write('Digite o endereco: ');
readln(wende);
Repeat
write('Deseja inserir os dados no <i>nicio ou no <f>im do arquivo: ');
readln(pos);
pos := Chr(Ord(pos)-32);
if ((pos <> 'I') and (pos <> 'F')) then
error;
until(pos = 'I') or (pos = 'F');
case pos of
'I' : begin
writeln('Dados Inseridos no Inicio do Arquivo');
if (nome[0] <> '') then
begin
for i:=0 to 9 do
begin
if i <> 9 then
begin
nome[9-i] := nome[8-i];
ende[9-i] := ende[8-i];
end;
end;
end;
nome[0] := wnome;
ende[0] := wende;
end;
'F' : begin
writeln('Dados Inseridos no Fim do Arquivo');
if (nome[9] <> '') then
begin
for i:=0 to 8 do
begin
nome[i] := nome[1+i];
ende[i] := ende[1+i];
end;
end;
nome[9] := wnome;
ende[9] := wende;
end;
end;
repeat
write('Deseja cadastrar novamente <S>im ou <N>ao: ');
readln(resp);
resp := Chr(Ord(resp)-32);
if ((resp <> 'S') and (resp <> 'N')) then
error;
until(resp = 'S') or (resp = 'N');
if (resp = 'S') then
i := i + 1;
end;
end;
procedure listar;
begin
for i:=0 to 9 do
begin
write(i);
writeln(' ' + nome[i] + '-' + ende[i]);
end;
writeln('Fim da lista');
readkey;
end;
{sistema principal}
begin
dir := 'C:\Pinga\Ed_Wilton_Lista1.txt';
Repeat
write('Deseja <C>riar ou <L>er um arquivo: ');
readln(resparq);
resparq := Chr(Ord(resparq)-32);
if ((resparq <> 'C') and (resparq <> 'L')) then
error;
until(resparq = 'C') or (resparq = 'L');
if resparq = 'L' then
LerArquivo;
textbackground(green);
textcolor(yellow);
while (opc < 5) do
begin
clrscr;
opc := 0;
menu;
readln(opc);
case opc of
1 : begin
writeln('Inserir Dados');
inserir;
end;
2 : writeln('Remover');
3 : begin
writeln('Listar Dados');
listar;
end;
4 : writeln('Pesquisar');
5 : begin
GravaArquivo;
writeln('SISTEMA ENCERRADO.OBRIGADO!');
end;
end;
if (opc > 5) then
begin
Error;
readkey;
end;
end;
readkey;
end.
O codigo é esse quem puder ajudar eu agradeço muito.
obrigado
Deus Abençoe a todos
Wilton Júnior
Curtidas 0
Respostas
Marcos Iwazaki
27/09/2010
amigo tente explicar melhor o que não funciona.
Tipo se ele esta fazendo errado, se não faz nada, se da erro.Se esta fazendo errado... o que ele tafazendo e o que deveria fazer...
Olhando assim meio por cimaNão deveria ser Readln em vez de Read?
flw
Tipo se ele esta fazendo errado, se não faz nada, se da erro.Se esta fazendo errado... o que ele tafazendo e o que deveria fazer...
Olhando assim meio por cimaNão deveria ser Readln em vez de Read?
flw
GOSTEI 0
Wilton Júnior
27/09/2010
Ao executar o programa o usuario seleciona se deseja criar o arquivo ou ler o arquivo, quando ele escolhe para ler o arquivo o programa fecha, não executa a função de ler o arquivo. Simplismente não da erro nenhum ele so não executa o reset, porque eu nã sei.
obrigado
obrigado
GOSTEI 0
Wilson Junior
27/09/2010
De uma olhada em http://forum.clubedohardware.com.br/resolvido-lendo-arquivos/762782?s=0b204107ef444e2e36bcc4c6bac291fb& e veja se lhe ajuda.
Espero ter colaborado.
Espero ter colaborado.
GOSTEI 0
Wilton Júnior
27/09/2010
Obrigado pela ajuda mas o problema ainda continua, Sr Wilson Lehapan.
GOSTEI 0
Wilson Junior
27/09/2010
Teste isto
Espero ter colaborado.
procedure percorreArquivoTexto ( nomeDoArquivo: String );
var
arq: TextFile;
linha: String;
begin
AssignFile ( arq, nomeDoArquivo );
Reset ( arq );
ReadLn ( arq, linha );
while not Eof ( arq ) do
begin
{ Processe a linha lida aqui. }
{ Para particionar a linha lida em pedaços, use a função Copy. }
ReadLn ( arq, linha );
end;
CloseFile ( arq );
end;
Espero ter colaborado.
GOSTEI 0
Wilton Júnior
27/09/2010
Sr. Wilson Lehapan, encontrou o que estava de errado agora meu sistema deu certo muito obrigado.
GOSTEI 0