Recent

Author Topic: Convert a specific part of a text file into a string  (Read 3221 times)

Peterkl

  • New Member
  • *
  • Posts: 22
Convert a specific part of a text file into a string
« on: October 07, 2015, 10:12:21 pm »
Greetings,

First of all, I'm not exaclty sure if what I'm trying to do is possible.

In fact, I want to convert into a string, a specific part of a text file ( per example : only the characters between 50-55) and after that write that string [I mean : writeln(thestring);]

Please note, that I do not know that specific part of the text file, and I am not looking for a specific string in the text neither, what I really need is to get a specific part of the text into a string, and in order to get that specific part I only know its placement in the txt file (the line, and placement in the line, even if I don't want to copy the whole line, only that specific part of the line)

I've tried a few things so far. I converted the whole txt file into a string :

...
var
 ikstring, two : string;
 lk : text
...
assign(ik, ikstring );
           reset(lk);
             repeat
             readln(lk, two);
            until eof(one); 

After this I tried to use a function to copy only a part of the "two" string :

function copy(two : string ;index:integer;count:integer) : string;
begin
two := copy(two, 5, 9);
writeln(two);
readln;
end;

It does not work, since I have to start with the function at the very beginning, and I only need in the middle of program.

I've also tried to create a "label" and to "goto" the program directly to the code of the function when it is actually needed, but since the function and the main program codes are separated it didn't work out. 

Thank you guys in advance, it's starting to seem impossible for me to do this  :p

P.S: I'm using win os, just noticed now I wrote on general and not on the win os section and I cannot delete the post. Sorry for that.
« Last Edit: October 07, 2015, 10:14:16 pm by Peterkl »

aidv

  • Full Member
  • ***
  • Posts: 173
Re: Convert a specific part of a text file into a string
« Reply #1 on: October 07, 2015, 11:20:15 pm »
Why not just use Copy()?

Code: Pascal  [Select][+][-]
  1. Copy(myString, 1, 50, 5);

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Convert a specific part of a text file into a string
« Reply #2 on: October 07, 2015, 11:31:07 pm »
You could try this:
Code: Pascal  [Select][+][-]
  1. program ExtractStringFromTextFile;
  2.  
  3. uses sysutils, Classes;
  4.  
  5. function GetStrFromTextFile(const aFilename: string;
  6.                             aLineNo, aLineOffset, aLength: integer): string;
  7. var
  8.   sl: TStringList;
  9.   idx, endPos: integer;
  10. begin
  11.   if not FileExists(aFilename) then
  12.     Exit('');
  13.   sl:=TStringList.Create;
  14.   try
  15.     sl.LoadFromFile(aFilename);
  16.     idx:=aLineNo - 1;
  17.     if not (idx < sl.Count) then
  18.       Exit('');
  19.     endPos:=aLineOffset + aLength - 1;
  20.     if (Length(sl[idx]) < endPos) then
  21.       Exit('');
  22.     Result:=Copy(sl[idx], aLineOffset, aLength);
  23.   finally
  24.     sl.Free;
  25.   end;
  26. end;
  27.  
  28. begin
  29.   WriteLn('Trying: "GetStrFromTextFile(''ExtractStringFromTextFile.lpr'',5,10,18)"');
  30.   WriteLn;
  31.   WriteLn('Result = "', GetStrFromTextFile('ExtractStringFromTextFile.lpr',5,10,18),'"');
  32.   ReadLn;
  33. end.

BitBangerUSA

  • Full Member
  • ***
  • Posts: 183
Re: Convert a specific part of a text file into a string
« Reply #3 on: October 08, 2015, 06:25:09 am »
without going too deep - seems like a programming class assignment - i'll point toward a small part...

assign(ik, ikstring );
           reset(lk);
             repeat
             readln(lk, two);
            until eof(one);

think about what the variable two will hold after last iteration of the readln line.
you mentioned knowing what line and position the characters you want to copy are at in the text file.
where is the code that gets you to that position in the text file?
Lazarus Ver 2.2.6 FPC Ver 3.2.2
Windows 10 Pro 64-bit

Peterkl

  • New Member
  • *
  • Posts: 22
Re: Convert a specific part of a text file into a string
« Reply #4 on: October 08, 2015, 09:11:58 pm »
Why not just use Copy()?

Code: Pascal  [Select][+][-]
  1. Copy(myString, 1, 50, 5);

First of all, thank you to the three of you.

This simple code, worked like a charm :)

 

TinyPortal © 2005-2018