Fórum Gravar no arquivo #458085
10/10/2013
0
Estou com problemas em uma implementação.
Tenho a procedure abaixo:
procedure TfrmPrincipal.PopulaNovoArquivo(var matriz: array of string; valor, novaLinha, arq: string);
var
i, j: integer;
ListaArray, lst: TStringList;
begin
ListaArray := TStringList.Create;
lst := TStringList.Create;
ListaArray.Clear;
lst.Clear;
for i := low(matriz) to High(matriz) do
begin
ExtractStrings(['|'], [], PChar(matriz[i]), ListaArray);
if ListaArray[0] = valor then
begin
matriz[i] := novaLinha;
end;
end;
ListaArray.Free;
for j := low(matriz) to High(matriz) do
begin
lst.Add(matriz[j]);
end;
lst.SaveToFile(arq);
lst.Free;
end;
O que esta procedure faz é o seguinte: Ele recebe um array e 3 campos string.
A ideia é pegar esse array, fazer una comparação pelo paramento 'valor' e, no índice(que é único), que for encontrado o 'valor', eu irei substitui o conteúdo desse índice pelo parâmetro 'novaLinha' e então, paro o loop for e já gravo todo o array novo em um arquivo txt.
2 erros estão acontecendo.
1) Aqui, deveria alterar o array, não esta alterando
for i := low(matriz) to High(matriz) do
begin
ExtractStrings(['|'], [], PChar(matriz[i]), ListaArray);
if ListaArray[0] = valor then
begin
matriz[i] := novaLinha;
end;
end;
2) Aqui, deveria salvar o conteúdo do array alterado no arquivo txt: acesso negado ao arquivo
for j := low(matriz) to High(matriz) do
begin
lst.Add(matriz[j]);
end;
lst.SaveToFile(arq);
lst.Free;
Como corrigir esses dois erros?
Detalhe o array esta testado e chegando Ok dentro da procedure. o problema é depois de passar pelo for.
Carlos Rocha
Curtir tópico
+ 0Posts
10/10/2013
Carlos Rocha
procedure TfrmPrincipal.PopulaNovoArquivo(var matriz: array of string;
valor, novaLinha, arq: string);
var
i, j: integer;
ListaArray, lst: TStringList;
begin
for i := low(matriz) to High(matriz) do
begin
ListaArray := TStringList.Create;
ListaArray.Clear;
ExtractStrings(['|'], [], PChar(matriz[i]), ListaArray);
if ListaArray[0] = valor then
begin
matriz[i] := novaLinha;
end;
end;
ListaArray.Free;
lst := TStringList.Create;
for j := low(matriz) to High(matriz) do
begin
lst.Add(matriz[j]);
end;
ShowMessage(lst.Text);
//lst.SaveToFile(arq);
lst.Free;
end;
Falta-me apenas a parte do acesso ao arquivo para altera-lo. Esta dando que não pode criar o arquivo.
Gostei + 0
10/10/2013
Ricardo Rodrigues
Gostei + 0
10/10/2013
Carlos Rocha
Estava abrindo o arquivo mas não chagava a fechar ele. Então, não tinha ´permissão de escrita.
procedure TfrmPesquisa.btnProcurarClick(Sender: TObject);
var
linha, funcionario: string;
Parte: TStringList;
arq: TextFile;
begin
AssignFile ( arq, Principal.fileName );
reset ( arq );
funcionario:=txtNome.text;
while not Eof ( arq ) do
begin
ReadLn ( arq, linha );
Parte := TStringList.Create;
Parte.Clear;
ExtractStrings(['|'],[], PChar(linha), Parte);
if (Parte[0]=funcionario) or (Parte[1]=funcionario) then
begin
nome:=Parte[0];
CPF:=Parte[1];
salario:=Parte[2];
idade:=Parte[3];
CloseFile (arq);
frmPesquisa.Hide();
frmFuncionarios.show();
Parte.Free;
abort;
end;
end;
ShowMessage('Nenhum resultado para sua Pesquisa!');
CloseFile (arq);
end;
eu preciso arranjar uma forma de sair dessa while sem abortar a operaçao de fora a chegar no closeFile (arq) lá embaixo
Gostei + 0
10/10/2013
Ricardo Rodrigues
Gostei + 0
10/10/2013
Carlos Rocha
só que, aparentemente, não fecha.
A ideia é, se encontrar uma combinação, no form de pesquisa, popular as variáveis e fechar o arquivo pois vou precisar dele aberto e não estou conseguindo fechar.
procedure TfrmPesquisa.btnProcurarClick(Sender: TObject);
var
linha, funcionario: string;
Parte: TStringList;
arq: TextFile;
begin
AssignFile ( arq, Principal.fileName );
reset ( arq );
funcionario:=txtNome.text;
while not Eof ( arq ) do
begin
ReadLn ( arq, linha );
Parte := TStringList.Create;
Parte.Clear;
ExtractStrings(['|'],[], PChar(linha), Parte);
if (Parte[0]=funcionario) or (Parte[1]=funcionario) then
begin
nome:=Parte[0];
CPF:=Parte[1];
salario:=Parte[2];
idade:=Parte[3];
CloseFile (arq);
frmPesquisa.Hide();
frmFuncionarios.show();
end;
Parte.Free;
exit;
end;
ShowMessage('Nenhum resultado para sua Pesquisa!');
CloseFile (arq);
end;
Testei o SaveToFile co outro arquivo e deu certo.
Gostei + 0
10/10/2013
Ricardo Rodrigues
Gostei + 0
10/10/2013
Carlos Rocha
procedure TfrmPesquisa.btnProcurarClick(Sender: TObject);
var
linha, funcionario: string;
Parte: TStringList;
arq: TextFile;
sair: boolean;
begin
sair:= false;
AssignFile ( arq, Principal.fileName );
reset ( arq );
funcionario:=txtNome.text;
while (not Eof ( arq )) and (sair=false) do
begin
ReadLn ( arq, linha );
Parte := TStringList.Create;
Parte.Clear;
ExtractStrings(['|'],[], PChar(linha), Parte);
if (Parte[0]=funcionario) or (Parte[1]=funcionario) then
begin
nome:=Parte[0];
CPF:=Parte[1];
salario:=Parte[2];
idade:=Parte[3];
sair:=true;
Parte.Free;
frmPesquisa.Hide();
frmFuncionarios.show();
end;
end;
if sair=false then
begin
ShowMessage('Nenhum resultado para sua Pesquisa!');
end;
CloseFile (arq);
end;
Sistinha para a Faculdade
http://www.guicapas.com.br/teste.zip
Gostei + 0
10/10/2013
Ricardo Rodrigues
repeat
until sair = true;
Gostei + 0
10/10/2013
Carlos Rocha
O problema é com o close(arq)
É um projeto (estudo) para a faculdade.
Pode baixalo aqui
[url]http://www.guicapas.com.br/teste.zip[/url]
Gostei + 0
10/10/2013
Ricardo Rodrigues
Gostei + 0
10/10/2013
Carlos Rocha
É Lazarus tá!
Facul só software livre
Taí
Tem u arquivo funcionarios.txt no diretório da aplicaçao
Gostei + 0
10/10/2013
Ricardo Rodrigues
procedure TForm1.Button1Click(Sender: TObject);
var
Arq : TextFile;
Texto : string;
Txt : TStringList;
achou : Boolean;
begin
Txt := TStringList.Create;
Txt.Delimiter := '|';
AssignFile(Arq,'Y:\Ricardo\teste\Ricardo\funcionarios.txt');
Reset(Arq);
memo1.clear;
if not EOF(Arq) then
repeat
ReadLn(Arq, Texto);
Txt.DelimitedText := Texto;
if Txt[0] = Edit1.Text then begin
Memo1.lines.Add('Nome : ' + txt[0]);
Memo1.lines.Add('Cpf : ' + txt[1]);
Memo1.lines.Add('Salario : ' + txt[2]);
Memo1.lines.Add('Idade : ' + txt[3]);
achou := true;
end;
until achou = true;
if not achou then begin
ShowMessage('Nenhum resultado para sua Pesquisa!');
CloseFile(arq);
end;
end;
Gostei + 0
10/10/2013
Carlos Rocha
procedure TfrmPesquisa.btnProcurarClick(Sender: TObject);
var
linha, funcionario: string;
Parte: TStringList;
arq: TextFile;
sair: boolean;
begin
sair:= false;
AssignFile ( arq, Principal.fileName );
reset ( arq );
funcionario:=txtNome.text;
while (not Eof ( arq )) and (sair=false) do
begin
ReadLn ( arq, linha );
Parte := TStringList.Create;
Parte.Clear;
ExtractStrings(['|'],[], PChar(linha), Parte);
if (Parte[0]=funcionario) or (Parte[1]=funcionario) then
begin
nome:=Parte[0];
CPF:=Parte[1];
salario:=Parte[2];
idade:=Parte[3];
sair:=true;
Parte.Free;
frmPesquisa.Hide();
frmFuncionarios.show();
end;
end;
if sair=false then
begin
ShowMessage('Nenhum resultado para sua Pesquisa!');
end;
CloseFile (arq);
end; Gostei + 0
10/10/2013
Ricardo Rodrigues
Gostei + 0
10/10/2013
Carlos Rocha
]Mas como você não corrigiu o meu código, antes criou outro similar, tentei adaptar teu codigo mas não esta dando nada ao clikar no botão enviar.
procedure TfrmPesquisa.btnProcurarClick(Sender: TObject);
var
arq : TextFile;
texto, nome, CPF, salario, idade, funcionario : string;
txt : TStringList;
achou : Boolean;
begin
txt := TStringList.Create;
txt.Delimiter := '|';
AssignFile(arq,Principal.fileName);
Reset(arq);
funcionario:=txtNome.text;
if not EOF(arq) then
repeat
ReadLn(arq, texto);
txt.DelimitedText := texto;
if txt[0] = funcionario then begin
nome := txt[0];
CPF := txt[1];
salario := txt[2];
idade := txt[3];
achou := true;
frmPesquisa.Hide();
frmFuncionarios.show();
end;
until achou = true;
if not achou then begin
ShowMessage('Nenhum resultado para sua Pesquisa!');
end;
CloseFile(arq);
end;
Mas, onde eu estou fechando o arquivo no codigo anterior?
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)