SRL Archive - OSR Help

help me please :)


Shuttleu

2010-04-02 18:09:13

right now lets start of by saying hi
i havent been here for a long time but i needed something done and i have become stuck
i have not opened scar for a good 4-5 months so i cannot remember if this is possible and if so how you do it but here is my question

i have a array of strings
textblah[0]
textblah[1]
textblah[2]
textblah[3]

this is a lot of text so it will probably go to a few billion
i want to know if i can use one command or a loop to save all of them to a text file
or save it to a text file then replace the var then save that on a new line on the text file then replace the var etc etc

any help is greatly appriciated

~shut

Frement

2010-04-02 18:25:40

File functions

function OpenFile(Path: string; Shared: Boolean): Integer;
Opens file for reading and returns file number if successful or negative number if failed. If shared = true then attempts to open files used by other applications as read-only.

function RewriteFile(Path: string; Shared: Boolean): Integer;
Opens file for writing and truncates it. Returns file number if successful or negative number if error.

procedure CloseFile(FileNum: Integer);
Closes file specified by FileNum.

function EndOfFile(FileNum: Integer): Boolean;
Returns true if end of file specified by FileNum reached.

function FileSize(FileNum: Integer): LongInt;
Returns file size in bytes.

function WriteFileByte(FileNum: Integer; b: Byte): Boolean;
Writes a single byte to file.

function WriteFileInt(FileNum: Integer; i: Integer): Boolean;
Writes a single integer to file.

function WriteFileString(FileNum: Integer; s: string): Boolean;
Writes a string to file.

function ReadFileByte(FileNum: Integer; var b: Byte): Boolean;
Reads a single byte from file.

function ReadFileInt(FileNum: Integer; var i: Integer): Boolean;
Reads a single integer from file.

function ReadFileString(FileNum: Integer; var s: string; Length: Integer): Boolean;
Reads Length number of bytes from file into string s.

procedure WriteINI(Section, KeyName, NewString, FileName: string);
Writes data to an ini-structured file.
Structure:
[Section]
KeyName=NewString

function ReadINI(Section, KeyName, FileName: string): string;
Reads data from an ini-structured file.
Structure:
[Section]
KeyName=SomeString

procedure DeleteINI(Section, KeyName, FileName: string);
Deletes a KeyName from an INI file and if you pass an empty string as KeyName it will delete then entire Section.

function DirectoryExists(Directory: string): Boolean;
Returns true if the directory exists.

function FileExists(FileName: string): Boolean;
Returns true if the file exists.

function ExtractFileName(FileName: string): string;
Returns the name of a file from a string.
Example:
C:\somefolder\somefile.ext => somefile.ext

function ExtractFilePath(FileName: string): string;
Returns the filepath from a string.
Example:
C:\somefolder\somefile.ext => C:\somefolder\

function ExtractFileExt(FileName: string): string;
Returns the file extension from a string.
Example:
C:\somefolder\somefile.ext => .ext

function ExtractFileDrive(FileName: string): string;
Returns the file drive from a string.
Example:
C:\somefolder\somefile.ext => C:

function GetFiles(Path, Ext: string): TStringArray;
Returns the files in a directory.

function GetFolders(Path: string): TStringArray;
Returns the folders in a directory.

function SaveToFile(Sender: TObject; FileName: string): Boolean;
Saves the content of certain objects:
TStrings, TStringList, TBitmap, TImage, TPicture, TMemo, TListBox, TComboBox, TRichEdit, TCheckListBox, TGraphic
Returns True if the saving was succesful.

function LoadFromFile(Sender: TObject; FileName: string): Boolean;
Loads the content of certain objects:
TStrings, TStringList, TBitmap, TImage, TPicture, TMemo, TListBox, TComboBox, TRichEdit, TCheckListBox, TGraphic
Returns True if the loading was succesful.

function MD5FromFile(Filepath: string): string;
Returns the MD5 hash value of a file.

function GetFileSize(FileName: string): Int64;
Returns the size of a file without opening it.

Mainly you need only:
function RewriteFile(Path: string; Shared: Boolean): Integer;

function WriteFileString(FileNum: Integer; s: string): Boolean;

procedure CloseFile(FileNum: Integer);

Shuttleu

2010-04-02 18:29:09

well i have this and it gave me a file access error
program New;

var
test:string;
i, thefile:integer;

begin
thefile:= OpenFile('C:\testing.txt', true);
for i:=0 to 10 do
begin
test:= inttostr(i);
WriteFileString(thefile, test);
end;
CloseFile(thefile);
end.
ao i thought someone else could make me one ;)

could it be because in not using RewriteFile?

~shut

EDIT: i closed scar and reopened it and the access error has gone
but nothing gets written to the file

Nava2

2010-04-02 18:37:26

Use RewriteFile

Sex

2010-04-02 18:42:30

And if you want to append, first use OpenFile and read the text and prepend it to the variable you are writing to the file.

Shuttleu

2010-04-02 18:43:06

good its writing now
now one problem… it doesnt write them on new lines
i have this
program New;

var
test:string;
i, thefile:integer;

begin
thefile:= RewriteFile('C:\testing.txt', false);
for i:=0 to 10 do
begin
test:= inttostr(i)+chr(32);
WriteFileString(thefile, test);
end;
CloseFile(thefile);
end.
the chr(32) was my attempt to make it put it on a new line, but it didnt work. it only made it make spaces :(

~shut

Naum

2010-04-02 18:45:04

Use #10+#13

So:
test:= inttostr(i)+#10+#13;

That should work :)

Shuttleu

2010-04-02 19:06:51

still nope… :(
owell i suppose scar will just have a lot of stuff in the debug window

~shut

Timer

2010-04-03 16:57:00

try this..
program New;

var
Restore: string;
i, TheFile, h: Integer;
WriteToFileS: TStringArray;

begin
WriteToFileS := [
'Hey',
'you',
'look',
'it',
'works!'
];

if (FileExists('C:\Testing.txt')) then
begin
TheFile := OpenFile('C:\Testing.txt', False);
ReadFileString(TheFile, Restore, FileSize(TheFile));
CloseFile(TheFile);
end;
TheFile := RewriteFile('C:\Testing.txt', False);
WriteFileString(TheFile, Restore);
h := High(WriteToFileS);
for i := 0 to h do
WriteFileString(TheFile, WriteToFileS[i] + Chr(13));
CloseFile(TheFile);
end.

i luffs yeww

2010-04-03 17:16:06

lolwut doesn't work, Timer.

Sex

2010-04-03 17:47:23

Code:
[LEFT]var
s, ss : string;
FileNum, h, i : integer;
Ex : TStringArray;
begin
s := 'Write\nThis\nTo\nFile';
Ex := Explode('\n', s);
ClearDebug;
h := high(Ex);
for i := 0 to h do
Writeln(Ex);
FileNum := RewriteFile('C:\wat.txt', False);
WriteFileString(FileNum, GetDebugText);
end.
[/LEFT]

To use your array of strings instead of exploding it just use your array.
If you want to use the explode, implode the array with \n as separator.