我需要用户编辑自定义文件格式。格式如下:
Line 1: 3 2290 //3 columns, 2290 lines, not processed
Line 2: "Index" "Primary" "Secondary" //Column names, not processed
Lines 3+: "Name" "Primary Action" "Secondary Action" // Processed
这是我的代码,显示我如何加载行,并将它们修剪为单元格:
var
Col2,Col3: AnsiString;
// At this point column 1 is already loaded into a ListBox
Procedure LoadColumns(Sender: TObject);
var
Lines: TArrayOfString;
Line: AnsiString;
P: Integer;
begin
LoadStringsFromFile(FileName, Lines);
Line := Lines[IndexList.ItemIndex + 2]; //Skip first 2 lines
P := Pos('" "', Line);
Delete(Line, 1, P + 1);
P := Pos('" "', Line);
Col2 := RemoveQuotes(Copy(Line, 1, P)); //Loaded Column2 of the line to _global_ variable Col2
Delete(Line, 1, P + 1);
Col3 := RemoveQuotes(Copy(Line, 1, Length(Line))); //Loaded Column3 of the line to _global_ variable Col3
Primary.Text := ConvertToCR(Col2);
Secondary.Text := ConvertToCR(Col3);
end;
Procedure SavetoPrimary(Sender: TObject);
var
Lines: TArrayOfString;
Line: String;
FromString, SaveString: AnsiString;
P, ChangeResult: Integer;
begin
LoadStringsFromFile(FileName, Lines);
Line := String(Lines[IndexList.ItemIndex + 2]);
FromString := ' "' + Col2 + '" ';
SaveString := ' "' + AnsiString(ConvertFromCR(Primary.Text)) + '" ';
ChangeResult := StringChangeEx(Line,FromString,SaveString,True)
if ChangeResult <> 1 then
begin
MsgBox('Your code sucks!', mbCriticalError, MB_OK);
end
else
begin
MsgBox('OK, that works.', mbInformation, MB_OK);
end;
end;
Procedure SavetoSecondary(Sender: TObject);
var
Lines: TArrayOfString;
Line: String;
SaveString: AnsiString;
P, ChangeResult: Integer;
begin
LoadStringsFromFile(FileName, Lines);
Line := String(Lines[IndexList.ItemIndex + 2]);
SaveString := AnsiString(ConvertFromCR(Secondary.Text));
ChangeResult := StringChangeEx(Line,Col3,SaveString,True)
if ChangeResult <> 1 then
begin
MsgBox('Your code sucks!', mbCriticalError, MB_OK);
end
else
begin
MsgBox('OK, that works.', mbInformation, MB_OK);
end;
end;
SaveToPrimary
不同,因为Index
和Primary
实际上可能是完全相同的字符串。
正如你所看到的那样,我实际上从未到过SaveStringToFile
,因为我不知道如何告诉它在特定地点保存。