1. SaveToFile adds newline at the end of the saved file -- is it intentional? How can I not write this newline?
2. I understood, perhaps erroneously, that it would be possible to save strings as ANSI, UTF-8 and even UTF-16; is it in fact possible now? How do I do this?
Please take your time; I don't need these answers right now.
How do I use SaveToFile in order to...?
How do I use SaveToFile in order to...?
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
- Rickard Johansson
- Site Admin
- Posts: 6783
- Joined: 19 Jul 2006 14:29
Re: How do I use SaveToFile in order to...?
Maybe streams are are a better choice. I've fixed the issues and I'll release v10.20 beta in a few days.
I've also made some updates to the help file.
You could use
I've also made some updates to the help file.
You could use
Code: Select all
// Read an UTF8 file to a string.
WORD mode = fmOpenRead;
TFileStream stream = TFileStream.Create("C:\\Test.txt", mode);
try
{
String s = stream.ReadString(s, encUTF8);
int len = Length(s);
...
}
finally
{
stream.Free;
};
// Write a string to an UTF8 file.
WORD mode = fmCreate or fmOpenReadWrite;
TFileStream stream = TFileStream.Create("C:\\Test.txt", mode);
try
{
String s = "Hello World!";
stream.WriteString(s, encUTF8);
}
finally
{
stream.Free;
};
Re: How do I use SaveToFile in order to...?
Thanks a lot! Alas, your code for reading (and also writing) stream doesn't work for me: I get "Not enough actual parameters" error on this line:
When I change int to var (since it's JavaScript, not Delphi), or simply comment out this line, I get "Incompatible types: 'TEncoding', 'TEncodings'" error (and no, it's not mismatched encodings error on my side, since the file I want to read has only Latin letters.)
By the way, how can I find out if a file I want to read doesn't exist? Is s variable simply null? (I want to append some text to an existing file.)
Code: Select all
int len = Length(s);
By the way, how can I find out if a file I want to read doesn't exist? Is s variable simply null? (I want to append some text to an existing file.)
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus
- Rickard Johansson
- Site Admin
- Posts: 6783
- Joined: 19 Jul 2006 14:29
Re: How do I use SaveToFile in order to...?
This code will work in v10.20.
I've added a few new functions. FileExists(), DirectoryExists() and FileSize().
Code: Select all
{
String s = "";
string sz = "C:\\Test.txt";
var size = FileSize(sz);
if (FileExists(sz)) {
Word mode = fmOpenReadWrite;
TFileStream stream = TFileStream.Create(sz, mode);
try
{
s = stream.ReadString(size, encAnsi);
s = s + "This text is appended to an existing text file...";
}
finally
{
stream.Free;
};
mode = fmOpenReadWrite;
} else {
mode = fmCreate or fmOpenReadWrite;
s = "New text file..."
}
// Write to the text file.
TFileStream stream = TFileStream.Create(sz, mode);
try
{
stream.WriteString(s, encAnsi);
}
finally
{
stream.Free;
};
}
Re: How do I use SaveToFile in order to...?
This is truly great! Thank you! So, I will finally be able to complete my handy (I hope so) script.
Alium tibi quaere fratrem; hic, quem tuum putas, meus est. Titus Flāvius Caesar Vespasiānus Augustus