2010-11-19 5 views
1

J'ai récemment commencé à programmer dans Delphi.Fichier de texte Delphi vide

Maintenant, j'ai un exercice.

Je dois enregistrer un top score dans un fichier texte. J'ai cette fonction:

{Voeg topscore toe aan het goede bestand.} 
function addTopScore(sudokuNumber : Integer; naam : String; tijd : Integer; fouten : Integer):boolean; 
var 
    buffer : TStringList; 
    aantal, aantal2, aantal3, i, i2, i3 : Integer; 
    scoreArray : array of TArray; 
    sameScoreArray : array of TArray; 
    plaatsScoreArray : array of Integer; 
    inputS1 : String; 
    zelfde, stopChecking : boolean; 
    insertBefore : Integer; 
    outputSL : TStringList; 
    outputNR : Integer; 
begin 
    {Haal de topscores op} 
    buffer := TStringList.Create; 
    buffer.LoadFromFile('scorelijst/lijst' + IntToStr(sudokuNumber)); 
    {Initialiseer variabelen} 
    aantal := 0; 
    aantal3 := 0; 
    aantal := (buffer.Count - 1); 
    zelfde := False; 
    result := True; 
    {Vul de score array (TStringList to Array)} 
    for i := 0 to aantal do 
    begin 
    SetLength(scoreArray, i + 1); 
    inputS1 := buffer[i]; 
    scoreArray[i] := Unit2.explode(',', inputS1, 0); 
    end; 

    {Controleer waar hij moet worden ingevoerd} 
    insertBefore := 0; 
    stopChecking := False; 
    for i2 := 0 to aantal do 
    begin 
    {Als er al een punt is gevonden, hoeft niet meer gecontroleerd te worden} 
    if(stopChecking = False) then 
    begin 
     {Als er een score van dezelfde persoon beter is, moet het result false zijn} 
     if (StrToInt(scoreArray[i2][1]) < fouten) AND (scoreArray[i2][1] = naam) then 
     begin 
     result := False; 
     end; 

     {Als het aantal fouten, hetzelfde is als de huidige waarde, sla de positie op} 
     if (StrToInt(scoreArray[i2][1]) = fouten) then 
     begin 
     {aantal zelfde waarden + 1} 
     aantal3 := aantal3 + 1; 

     {Geef de arrays de goede lengte} 
     SetLength(sameScoreArray, aantal3); 
     SetLength(plaatsScoreArray, aantal3); 

     {Vul de arrays} 
     sameScoreArray[(aantal3 - 1)] := scoreArray[i2]; 
     plaatsScoreArray[(aantal3 - 1)] := i2; 

     {Er is een zelfde waarde gevonden.} 
     zelfde := True; 
     end; 

     {Als het aantal fouten groter is, dan de nieuwe} 
     if ((StrToInt(scoreArray[i2][1]) > fouten) = True) then 
     begin 
     {Stop de for loop checking} 
     stopChecking := True; 

     {Als er geen zelfde waarde is gevonden, moet hij voor deze i2 worden ingevoerd} 
     if (zelfde = False) then 
      insertBefore := i2; 
     end; 
    end; 
    end; 

    outputSL := TStringList.Create; 
    if (insertBefore > 0) then 
    begin 
    outputNR := 0; 
    for i3 := 0 to aantal do 
    begin 
     if (i3 = insertBefore) then 
     begin 
     outputSL[outputNR] := naam + ',' + IntToStr(fouten) + ',' + IntToStr(tijd); 
     outputNR := outputNR + 1; 
     end; 

     outputSL[outputNR] := scoreArray[i3][0] + ',' + scoreArray[i3][1] + ',' + scoreArray[i3][2]; 
     outputNR := outputNR + 1; 
    end; 
    end 
    else if (zelfde = True) then 
    begin 
    //Not finished. 
    end; 

    outputSL.SaveToFile('scorelijst/lijst' + IntToStr(sudokuNumber)); 
end; 

L'entrée (scorelijst/lijst1):

test,2,10 
test,3,11 

Maintenant, je veux trier le nouveau score, au bon endroit dans le fichier texte. Mais, la sortie est vide ... Qu'est-ce que je fais mal?

Les commentaires sont en néerlandais, désolé pour cela.

TArray = array of string;

Répondre

2

Est-ce que Delphi 6 a TStringList.CustomSort? Si c'est le cas, créez votre propre routine de tri et utilisez-la pour trier la StringList.

0

Vous devez simplement laisser le TStringList se sorte. i.e .:

sortieSL.Sortie: = true;