Dwa pytania

0

Dzień dobry, jak przełożyć tablice char'ów (Array of Char)
do stringa zrobiłem tak:

for i := 0 to length(tablica) - 1 do
string := string + tablica[i];

ale zastanawiam się czy jest może jakieś profesjonalne
rozwiązanie, które działało by szybciej, bo to jest długi string.

No i jeszcze jedno, słyszałem o jakiejś zminnej Variant w której
można przetrzymywać wszystko, czy jak by w niej był string
to czy zmieści się więcej znaków niż w normalnym stringu?--__________________________________
Pozdrawiam...
piechnat

0

hmm...troszke nie jasno piszesz

Nie wiem czy istnieje jakas inna 'profesjonalna' metoda rozwiazania twego problemu.

NIe bardzo wiem tez co rozumiesz przez dlugi string...?

jesli piszesz w Delphi to o ile sie nie myle String moze miec wielkosc 2 GB, czy to malo?;)

a tekst ponizszy chyba dotyczy tego o czym mowisz...

Sometimes it is necessary to manipulate data whose type varies or cannot be determined at compile time. In these cases, one option is to use variables and parameters of type Variant, which represent values that can change type at runtime. Variants offer greater flexibility but consume more memory than regular variables, and operations on them are slower than on statically bound types. Moreover, illicit operations on variants often result in runtime errors, where similar mistakes with regular variables would have been caught at compile time. You can also create custom variant types.

By default, Variants can hold values of any type except records, sets, static arrays, files, classes, class references, and pointers. In other words, variants can hold anything but structured types and pointers. They can hold interfaces, whose methods and properties can be accessed through them. (See Object interfaces.) They can hold dynamic arrays, and they can hold a special kind of static array called a variant array. (See Variant arrays.) Variants can mix with other variants and with integer, real, string, and Boolean values in expressions and assignments; the compiler automatically performs type conversions.

Variants that contain strings cannot be indexed. That is, if V is a variant that holds a string value, the construction V[1] causes a runtime error.
You can define custom Variants that extend the Variant type to hold arbitrary values. For example, you can define a Variant string type that allows indexing or that holds a particular class reference, record type, or static array. Custom Variant types are defined by creating descendants to the TCustomVariantType class.

A variant occupies 16 bytes of memory and consists of a type code and a value, or pointer to a value, of the type specified by the code. All variants are initialized on creation to the special value Unassigned. The special value Null indicates unknown or missing data.
The standard function VarType returns a variant’s type code. The varTypeMask constant is a bit mask used to extract the code from VarType’s return value, so that, for example,

VarType(V) and varTypeMask = varDouble

returns True if V contains a Double or an array of Double. (The mask simply hides the first bit, which indicates whether the variant holds an array.) The TVarData record type defined in the System unit can be used to typecast variants and gain access to their internal representation. See the online Help on VarType for a list if codes, and note that new type codes may be added in future implementations of Object Pascal.
--take care,

Wodzu

0

Chce po prostu pobrać zawartość pliku do stringa, i robie to tak:

function czytaj_plik(plik: string): string;
var
tablica : array of char;
f: file of char;
i: integer;
s: string;
begin
assignfile(F, plik);
reset(F);
setlength(tablica, filesize(F));
blockread(F, tablica[0], filesize(F));
CloseFile(F);
for i := 0 to length(tablica) - 1 do
s := s + tablica[i];
Result := s;
end;

no i chodzi mi o to czy to nie jest może jakieś naciągane rozwiązanie,
bo buforów to ja mało używałem, no i nie wiem czy to zawsze i szybko zadziała.
Czy może nie ma rozwiązanie specjalnie do tego zaplanowanego, tak jak np.
w PHP można czytać plik po linijce, ale można też:
$plik = implode("", file("plik.txt"));

&gtNIe bardzo wiem tez co rozumiesz przez dlugi string...?
&gtjesli piszesz w Delphi to o ile sie nie myle String moze miec wielkosc 2 GB, czy to malo?

:) to się cisze, bo ktoś ostatnio napisał mi że string ma maksymalnie 255 KB
--__________________________________
Pozdrawiam...
piechnat

0

piechnat napisał:
Chce po prostu pobrać zawartość pliku do stringa, i robie to tak:
&gt
&gtfunction czytaj_plik(plik: string): string;
&gtvar
&gt tablica : array of char;
&gt f: file of char;
&gt i: integer;
&gt s: string;
&gtbegin
&gt assignfile(F, plik);
&gt reset(F);
&gt setlength(tablica, filesize(F));
&gt blockread(F, tablica[0], filesize(F));
&gt CloseFile(F);
&gt for i := 0 to length(tablica) - 1 do
&gt s := s + tablica[i];
&gt Result := s;
&gtend;
&gt
&gtno i chodzi mi o to czy to nie jest może jakieś naciągane rozwiązanie,
&gtbo buforów to ja mało używałem, no i nie wiem czy to zawsze i szybko zadziała.
&gtCzy może nie ma rozwiązanie specjalnie do tego zaplanowanego, tak jak np.
&gtw PHP można czytać plik po linijce, ale można też:
&gt$plik = implode("", file("plik.txt"));
&gt
&gt&gtNIe bardzo wiem tez co rozumiesz przez dlugi string...?
&gt&gtjesli piszesz w Delphi to o ile sie nie myle String moze miec wielkosc 2 GB, czy to malo?
&gt
&gt:) to się cisze, bo ktoś ostatnio napisał mi że string ma maksymalnie 255 KB
&gt
&gt

  1. 255 bajtów ma ogranicznie ShorString, pozostałe (AnsiString i WideString mają 2GB)
  2. Z tego co widzę, to chcesz odczytać zawartość pliku do stringa. Nie szybciej byłoby tak ?:

Result:=string(tablica);

zamiast:

for i := 0 to length(tablica) - 1 do
s := s + tablica[i];
Result := s;
--Jest jeszcze jeden błąd ... :)

0

A po co Ci ta tablica???

function czytaj_plik(plik: string): string;
var
F:file of char; * rownie dobrze moze byc of byte
begin
assignfile(F, plik);
reset(F);
setlength(Result, filesize(F));
blockread(F, Result[1], filesize(F)); * zwroc uwage: 1 a nie 0 !!!
CloseFile(F);
end;

A do przepisywania tablicy do stringa (i w ogole czegokolwiek do czegokolwiek) mozesz tez uzyc funkcji Move.

--
Pawel {Delphi 6 Personal}

Po pierwsze: naciśnij F1

0

Dzięki, właśnie o takie profesjonalniejsze rozwiązania mi chodziło :)--__________________________________
Pozdrawiam...
piechnat

1 użytkowników online, w tym zalogowanych: 0, gości: 1