How to copy a. txt encoded file to another decoded file using Assignfile?

Asked

Viewed 133 times

0

I have a. txt file encoded and need to copy the decoded content to another . txt.

In the first file, I have the following:

Gcbqrm5mnp4

Gcbqrm5mnp8

Gcbqrm5mnpc

When the new file is created, it is returning the correct number of lines, but only with the first item of the decoded list:

Bitmap_1

Bitmap_1

Bitmap_1

And the comeback should be:

Bitmap_1

Bitmap_2

Bitmap_3

The code I’m using:

var
arq, arqDec : TextFile;
linha, nome : string;
s : Integer;

     begin  //Decodifica o texto do arquivo
     AssignFile(arq, FrmCadastro.CBUsuario.Text +'-images.txt');
     Reset(arq);
     while not Eof(arq) do
       begin
       Readln(arq, linha);
       s := Pos(Encode64('Bitmap_'), linha);
       nome := Copy(linha, 1, s-1);
       Delete(linha, 1, s);
       ShowMessage(Decode64(linha)); //<--Até aqui dá o retorno correto (ShowMessage usado só pra testar)
       end;

       begin
       AssignFile(arqDec, FrmCadastro.CBUsuario.Text +'-Decod-images.txt');
       if not (FileExists(FrmCadastro.CBUsuario.Text +'-Decod-images.txt')) then
       Rewrite(arqDec)
       else
         Begin
         Append(arqDec);
         Writeln(arqDec, Decode64(linha));                
       end;
     end;

     CloseFile(arq);
     CloseFile(arqDec);
    end;

What am I doing wrong? Or what is missing to record the correct data in the new file . txt?

  • Nelson, you are writing in the file outside the loop that reads the source file. Try to improve the endentation that is easy to notice, ok?

2 answers

1


Nelson, if you want to create a file with the content displayed by Showmessage, then I think you should create that file at the beginning, before looping, and run Writeln at the same point where you added Showmessage:

var
  arq, arqDec : TextFile;
  linha {, nome} : string;
  s : Integer;
begin  //Decodifica o texto do arquivo
  AssignFile(arqDec, FrmCadastro.CBUsuario.Text +'-Decod-images.txt');
  Rewrite(arqDec);
  try
    AssignFile(arq, FrmCadastro.CBUsuario.Text +'-images.txt');
    Reset(arq);
    try
      while not Eof(arq) do
        begin
          Readln(arq, linha);
          s := Pos(Encode64('Bitmap_'), linha);
          // nome := Copy(linha, 1, s-1);
          Delete(linha, 1, s);
          // ShowMessage(Decode64(linha)); //<--Até aqui dá o retorno correto (ShowMessage usado só pra testar)
          Writeln(arqDec, Decode64(linha));                
        end;
    finally
      CloseFile(arq);
    end;
  finally
    CloseFile(arqDec);
  end;
end;

I hope it helps

0

You have a clear indentation problem.

Note this part of your code:

 while not Eof(arq) do
   begin
   Readln(arq, linha);
   s := Pos(Encode64('Bitmap_'), linha);
   nome := Copy(linha, 1, s-1);
   Delete(linha, 1, s);
   ShowMessage(Decode64(linha)); //<--Até aqui dá o retorno correto (ShowMessage usado só pra testar)
   end;

The right thing would be:

 while not Eof(arq) do
 begin
   Readln(arq, linha);
   s := Pos(Encode64('Bitmap_'), linha);
   nome := Copy(linha, 1, s-1);
   Delete(linha, 1, s);
   ShowMessage(Decode64(linha)); //<--Até aqui dá o retorno correto (ShowMessage usado só pra testar)
 end;

Whatever is inside the Begin and end; of while not Eof(Arq) do will be executed in repetition.

So try:

 while not Eof(arq) do
 begin
   Readln(arq, linha);
   s := Pos(Encode64('Bitmap_'), linha);
   nome := Copy(linha, 1, s-1);
   Delete(linha, 1, s);
   ShowMessage(Decode64(linha)); //<--Até aqui dá o retorno correto (ShowMessage usado só pra testar)

   AssignFile(arqDec, FrmCadastro.CBUsuario.Text +'-Decod-images.txt');

   if not (FileExists(FrmCadastro.CBUsuario.Text +'-Decod-images.txt')) then
     Rewrite(arqDec)
   else
   begin
     Append(arqDec);
     Writeln(arqDec, Decode64(linha));                
   end;
 end;
  • I still have a lot to learn... the mistake was really the endentation. @Imex, your code worked, but there is a problem, when it is a new file does not write on time, I need to close the program (just change screen to get another file does not solve) and only when I open again it saves the decoded file. But I don’t know if it’s my PC or program problem itself, I’ll test a little more.

  • Junior Moreira, your code gives me a return of "I/O error 32" and does not save the decoded file, must have missed something, but I did not have time to see yet.

  • Nelson, I don’t think this should happen. Make sure that Closefile is running (I will edit the previously suggested code to add a Try / Finally / end and thereby ensure the execution of Closefile) and that the file is not being written to a protected folder

  • @Imex, with Try / Finally / end the more code is working right. Despite being a silly doubt (I recognize), and that many ignore and think that it does not need to be answered, helped a lot. I am learning on my own to work on Delphi and even researching and reading a lot I still have some difficulty. Thank you so much for all your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.