Zdravim
Mam problem ve zpracovani txt souboru. Potreboval bych cisla v takovem souboru:
Backscattered Particles: angular range: 27.30 - 29.30 deg
Energy [keV] # of particles
--------------------------------
0.400 788
0.416 758
0.432 746
0.448 819
0.464 792
prevezt do real arraye, ale nevim, jak preskocit mezery.
Diky za pomoc
Fórum › Delphi
Zapracovani txt souboru

mno v delphi moc neumim ale zkus to nejak zbastlit pomoci prikazu copy a jako parametr dej že chceš od znaku kde je mezera do mezery....
Načítanie a výpis môže byť aj pomocou ObjectListu - ale najlepšie do nejakej SQL mem databázy (SQLite?).
Príklad načítania a spracovanie zoznamu pastujem. Nie je to však vhodné použiť na obrovitánske textové súbory. Pracuje to na princípe:
1. načítaj TXT
2. preskoč 3 riadky
3. aktuálny riadok rozdel na dve čísla, ktoré následne načítaj
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Contnrs;
resourcestring
// ---------------------------------------------------------------------------
sSuborNeexistuje = 'Subor "%s" neexistuje!';
sChybnyObsahSuboru = 'Súbor neobsahuje údaje!';
type
// ---------------------------------------------------------------------------
// --- TStruktura ------------------------------------------------------------
// ---------------------------------------------------------------------------
TStruktura = class
public
Energia: Double;
Zloziek: Integer;
end; // TStruktura
// ---------------------------------------------------------------------------
// --- TForm1 ----------------------------------------------------------------
// ---------------------------------------------------------------------------
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
FZoznam: TObjectList;
FFormatSettings: TFormatSettings;
protected
function NaplnZoznam(const NazovSuboru: String): Boolean;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end; // TForm1
var
Form1: TForm1;
implementation
{$R *.dfm}
// *****************************************************************************
procedure TForm1.Button1Click(Sender: TObject);
var
Index: Integer;
begin
if NaplnZoznam('File1.txt') then
begin
ShowMessage('Nacitanie prebehlo uspesne, vypisujem obsah zoznamu:');
// Zobrazi zaznam po zazname
for Index := 0 to Pred(FZoznam.Count) do
begin
with TStruktura(FZoznam[Index]) do
ShowMessage(Format('%d. riadok: %0.3f = Energia; %d = Zloziek', [Succ(Index), Energia, Zloziek]));
end; // FOR
end; // IF
end;
// *****************************************************************************
function TForm1.NaplnZoznam(const NazovSuboru: String): Boolean;
const
SeparatorCisel = #32; // Medzera
var
ChybneRiadky, ObsahSuboru: TStringList;
Index: Integer;
Riadok, Cislo: String; // Optimalizacia
begin
Result := FileExists(NazovSuboru);
if (not Result) then
raise Exception.CreateResFmt(@sSuborNeexistuje, [NazovSuboru]);
// Samotne nacitanie do StringListu
FZoznam.Clear;
ChybneRiadky := TStringList.Create;
ObsahSuboru := TStringList.Create;
try // FINALLY
ObsahSuboru.LoadFromFile(NazovSuboru); // Nacitame kompatibilny textovy subor
Result := ObsahSuboru.Count > 3;
if (not Result) then
raise Exception.CreateRes(@sChybnyObsahSuboru);
FFormatSettings.DecimalSeparator := '.'; // Separator desatinnych cisel ma byt "bodka"
// Preskocime prve tri riadky a do vsetky riadky natlacime do struktury
for Index := 3 to Pred(ObsahSuboru.Count) do
begin
// Pridame riadok (vytvorime objekt, tento vratime smernik na tento objekt
// a pretypujeme ho na objekt o ktorom vieme, ze je)
with TStruktura(FZoznam[FZoznam.Add(TStruktura.Create)]) do
begin
Riadok := TrimLeft(ObsahSuboru[Index]); // Odrezeme prazdne znaky na zaciatku riadka
// Parsujeme hodnotu energie
Cislo := Copy(Riadok, 1, Pred(Pos(SeparatorCisel, Riadok))); // Prve cislo
if (not TryStrToFloat(Cislo, Energia, FFormatSettings)) then
begin
Energia := 0.0; // Ak sa nepodari rozparsovat prve cislo, hodi sa 0
ChybneRiadky.Add(Trim(ObsahSuboru[Index])); // Odpamatame chybny riadok
end; // IF
// Parsujeme hodnotu poctu zloziek
Cislo := Trim(Copy(Riadok, Pos(SeparatorCisel, Riadok), MaxInt));
if (not TryStrToInt(Cislo, Zloziek)) then
begin
Zloziek := 0; // Ak by sa nepodarilo rozparsovat druhe cslo, hodi sa 0
ChybneRiadky.Add(Trim(ObsahSuboru[Index])); // Odpamatame chybny riadok
end; // IF
end; // WITH
end; // FOR
// Spracujeme chybne riadky - napr. len vypiseme
Result := ChybneRiadky.Count = 0;
if (not Result) then
begin
ChybneRiadky.Delimiter := #13; // Nie je to relevantny EOLN, ale kaslat na to
raise Exception.Create(ChybneRiadky.DelimitedText);
end; // IF
finally
FreeAndNil(ObsahSuboru);
FreeAndNil(ChybneRiadky);
end; // FINALLY
end;
// *****************************************************************************
constructor TForm1.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FZoznam := TObjectList.Create;
// Nacitame nastavenie podla OS (jazykova mutacia apod.)
GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, FFormatSettings); // Systemove formaty
end;
// *****************************************************************************
destructor TForm1.Destroy;
begin
FreeAndNil(FZoznam);
inherited Destroy;
end;
end.

Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 22 hostů
Podobná vlákna
Výpis souboru do txt — založil Odin
Přepsání txt souboru — založil Ramisha
Zapisování od txt souboru — založil M4rty32
Zápis do .txt souboru — založil Pavel
Export do txt souboru — založil matouch