How to move the cursor to the beginning of the next line on a .txt file?

88 Views Asked by At

I am writing a function with which, I'm trying to load saved data from a file to an array of records. The file is going to contain a name of the type of data I want to read. After that initial line every next line, until the end of the current type of data, will be a line of data that I need to fill into the array of records. Since I'm going to have different types of data lines, I don't want to iterate through the whole file, but I want to go straight to the lines I need to take data from. I have another function CheckTXTFileForDataName that searches if the data type exists in the file and returns an array of records, that returns a boolean, which value is based on whether the data type is in the file and it also returns the line at which the data type was found at, as an integer.

This is my code, with which I'm trying to load the data (Note: The function isn't finished, because I need to figure how to move the cursor first)

function LoadArrayFromTXTFile(const dataArray : array of TdataRecord; const arrayName : string) : TResultArray;
var
  txtFile : TextFile;
  txtFilePath, fileLine : string;
  i : integer;
begin
  txtFilePath := GetFilePath('Date Arrays\DataArrays.txt');

  AssignFile(txtFile, txtFilePath);

  if FileExists(txtFilePath) then
  begin
    Reset(txtFile);
  end;

  ReadLn(txtFile, fileLine);
  Delete(fileLine, Pos(' |', fileLine), (Length(fileLine) - Pos('|', fileLine) + 1));

  if CheckTXTFileForDataName(dataArray, arrayName)[0].nameIsInFile then
  begin                                                                              
    for I := 0 to Length(ReaderArray) - 1 do
    begin
      ReadLn(txtFile, fileLine);

      Result[i].keycode := StrToInt(Copy(fileLine, 1, (Pos(',', fileLine) - 1)));
      Result[i].shiftState := Copy(fileLine, Pos(' ', fileLine) + 1, Length(fileLine) - Pos(' ', fileLine));
    end;
  end;
end;

As you can see in the second if statement I'm reading a line from a text file and then I assign the data values to where I need them. My question is, can I, and if yes, how to change the cursor to a specific line of the file? As I already mentioned CheckTXTFileForDataName(dataArray, arrayName) has a second variable in it, which is the variable lineNumber with which I keep the line where the data starts, meaning if the lineNumber is 56, then I need to start reading from 57th line.

Here's an example of how the .txt file would look like with the names of the variables for you to understand what I'm saving(in brackets in what types I would be loading the data):

KeycodesArrayName(string) | KeycodeCount(integer)
keycode(word), shiftState(boolean)
keycode(word), shiftState(boolean)
.
.
.
keycode(word), shiftState(boolean)

KeycodesArrayName is the name with which I think of distinguishing the different saves of data. KeycodeCount is the amount of keycodes saved(also the amount of lines which I need to take after the correct data name). Keycode and shiftState are self-explanatory.

Here's an example with values:

FirstArray | 58
48, false
53, false
76, true
.
.
.
72, false

In this situation, if I want the data from FirstArray(I will name them better, this is a name just for the example), I will check if it exists in the file, and if yes, I'd save the amount of keycodeCount, which is 58, which means that I need the next 58 lines. Then the keycodes are just going to be saved as they are, not with Char() or anything like that, and same goes with shiftState, no changes there either.

0

There are 0 best solutions below