Recent

Author Topic: Read ASCII file  (Read 4260 times)

leveltwo

  • New member
  • *
  • Posts: 7
Read ASCII file
« on: April 20, 2015, 11:58:44 am »
Hey guys,

we want to read an ASCII file and convert it to hex and write the hex code in a text file.

   
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
  AssignFile(File1, 'C:\users\thomas\desktop\bild.jpg');
  Reset(File1);
    while not eof(File1) do
    begin
        readln(File1,s);
        AppendStr(s2, s);
    end;
   CloseFile(File1);

  AssignFile(File2,'C:\users\thomas\desktop\test.txt');
  Rewrite(File2);
  writeln(File2,s2);
  CloseFile(File2);   

the problem is in our ASCII code is SUB which means EOF.

Hopefully someone knows how to do it :D

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Read ASCII file
« Reply #1 on: April 20, 2015, 12:27:37 pm »
Code: [Select]
procedure TForm1.FormCreate(Sender: TObject);
begin
  AssignFile(File1, 'C:\users\thomas\desktop\bild.jpg');
  Reset(File1);
    while not eof(File1) do
    begin
        readln(File1,s);
        AppendStr(s2, s);

That filename to me suggests that it is not an ASCII file at all, so this will fail.

the problem is in our ASCII code is SUB which means EOF

I have no idea what you mean by that.

You can attach the project (sources zipped) and the file in question along with an example of the desired outputformat (in file2), so we can see what you are trying to achieve.

Bart

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Read ASCII file
« Reply #2 on: April 20, 2015, 12:55:09 pm »
Are you looking for something like the following? As Bart says, it will fail horribly with binary files.

Code: [Select]
procedure ConvertAscToHexStr(const aFileName: string);
var
  sl, conv: TStringList;
  s: string;

  function Converted: string;
  var
    p, len: integer;
  begin
    len:=Length(s);
    Result:='';
    if (len = 0) then Exit;
    for p:=1 to len do
      AppendStr(Result, IntToHex(Ord(s[p]),2));
  end;

begin
  sl:=TStringList.Create;
  try
    sl.LoadFromFile(aFileName);
    conv:=TStringList.Create;
    try
      for s in sl do
        conv.Append(Converted);
      conv.SaveToFile(aFileName + '.converted');
    finally
      conv.Free;
    end;
  finally
    sl.Free;
  end;
end;

fred

  • Full Member
  • ***
  • Posts: 201
Re: Read ASCII file
« Reply #3 on: April 20, 2015, 12:56:59 pm »
SUB as EOF is normal for a TextFile, i think it goes back to the MS-DOS days.
If you want to read a binary file then perhaps you should use the type "file" or "file of byte".

leveltwo

  • New member
  • *
  • Posts: 7
Re: Read ASCII file
« Reply #4 on: April 20, 2015, 03:37:22 pm »
We solve the problem with a TStringList.

now we want to search backwards in the string like the lastIndexOf method in java

:D

fred

  • Full Member
  • ***
  • Posts: 201

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Read ASCII file
« Reply #6 on: April 20, 2015, 07:55:41 pm »
We solve the problem with a TStringList.

Are you sure the .jpg (!!) file is a text file (ASCII as you say)?
Can you open it in notepad and see normal text??
Otherwise all and everything you try with either readln() or using a TStringList will fail miserably.

Bart

leveltwo

  • New member
  • *
  • Posts: 7
Re: Read ASCII file
« Reply #7 on: April 21, 2015, 08:41:41 am »
http://lazarus-ccr.sourceforge.net/docs/rtl/strutils/rpos.html

we dont have the RPos, RPosEx function. Where can we get it?

EDIT: Solve it.
« Last Edit: April 21, 2015, 09:02:59 am by leveltwo »

 

TinyPortal © 2005-2018