Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Loading a file with the LoadFromFile() function with a newline



Permlink Replies: 5 - Last Post: Aug 30, 2017 4:59 PM Last Post By: Remy Lebeau (Te...
duf _

Posts: 28
Registered: 7/2/06
Loading a file with the LoadFromFile() function with a newline
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 30, 2017 1:40 PM
I load the text file .txt using the LoadFromFile () function, and the text in the middle of the line is marked with a newline '\n'.
The LoadFromFile () function treats this character as a new line and divides the line in that place by creating a new line.
In the Windows system Note the text looks like this: Ala has ace
The program that loads this file looks different:

plik->LoadFromFile( path,  TEncoding::ASCII);
 
for( short int i = 0; i < plik->Count; ++i )
   Memo1->Lines->Add( plik->Strings[i] );


In Memo1 the text looks like this:

Ala
has ace

Can I remove the '\n' character to make the entire line and how?
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Loading a file with the LoadFromFile() function with a newline
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 30, 2017 2:06 PM   in response to: duf _ in response to: duf _
duf _ wrote:

I load the text file .txt using the LoadFromFile () function

For which component exactly?

and the text in the middle of the line is marked with a newline '\n'.
The LoadFromFile () function treats this character as a new line and
divides the line in that place by creating a new line.

What is plik declared as?

Assuming plik is a TStrings descendant, then its LoadFrom...() methods
treat bare-CR, bare-LF, and CRLF line breaks equally when the
TStrings::LineBreak property matches the RTL's global sLineBreak
variable. If the LineBreak property does not match sLineBreak, then
TStrings only splits on line breaks that match its LineBreak property.

for( short int i = 0; i < plik->Count; ++i )

Why are you using 'short int'? RTL/VCL Count properties are 'int'
instead.

Memo1->Lines->Add( plik->Strings[i] );

Too bad you can't use use the TMemo's own LoadFromFile() method in this
situation. Unfortunately, it normalizes all line breaks to CRLF before
passing the data to the Win32 API.

Can I remove the '\n' character to make the entire line and how?

That depends on what plik is. But probably no. You will likely have
to parse the file yourself so you can split its lines however you want.

--
Remy Lebeau (TeamB)
duf _

Posts: 28
Registered: 7/2/06
Re: Loading a file with the LoadFromFile() function with a newline
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 30, 2017 2:36 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
duf _ wrote:

I load the text file .txt using the LoadFromFile () function

For which component exactly?

TStrings

and the text in the middle of the line is marked with a newline '\n'.
The LoadFromFile () function treats this character as a new line and
divides the line in that place by creating a new line.

What is plik declared as?

TStrings* plik = new TStringList;

Assuming plik is a TStrings descendant, then its LoadFrom...() methods
treat bare-CR, bare-LF, and CRLF line breaks equally when the
TStrings::LineBreak property matches the RTL's global sLineBreak
variable. If the LineBreak property does not match sLineBreak, then
TStrings only splits on line breaks that match its LineBreak property.

for( short int i = 0; i < plik->Count; ++i )

Why are you using 'short int'? RTL/VCL Count properties are 'int'
instead.

As there is no need to use whole range of int

Memo1->Lines->Add( plik->Strings[i] );

Too bad you can't use use the TMemo's own LoadFromFile() method in this
situation. Unfortunately, it normalizes all line breaks to CRLF before
passing the data to the Win32 API.

So how what to use?

Can I remove the '\n' character to make the entire line and how?

That depends on what plik is. But probably no. You will likely have
to parse the file yourself so you can split its lines however you want.

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Loading a file with the LoadFromFile() function with a newline
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 30, 2017 3:03 PM   in response to: duf _ in response to: duf _
duf _ wrote:

TStrings* plik = new TStringList;

OK, then what I said earlier applies.

Why are you using 'short int'? RTL/VCL Count properties are 'int'
instead.

As there is no need to use whole range of int

Even so, you are forcing the compiler to promote the 'i' variable to
'int' on every loop iteration when it compares 'i' to the Count. It is
better to declare 'i' as 'int' to avoid that promotion, even if you
don't use the whole integer range. Besides, you never know, maybe your
files will grow over time to over 32767 lines.

So how what to use?

Since the RTL's sLineBreak constant is CRLF on Windows, and you don't
want to split on bare-LF line breaks, you are going to have to parse
the file data manually, not use LoadFromFile() at all.

--
Remy Lebeau (TeamB)
duf _

Posts: 28
Registered: 7/2/06
Re: Loading a file with the LoadFromFile() function with a newline
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 30, 2017 3:30 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
duf _ wrote:

TStrings* plik = new TStringList;

OK, then what I said earlier applies.

Why are you using 'short int'? RTL/VCL Count properties are 'int'
instead.

As there is no need to use whole range of int

Even so, you are forcing the compiler to promote the 'i' variable to
'int' on every loop iteration when it compares 'i' to the Count. It is
better to declare 'i' as 'int' to avoid that promotion, even if you
don't use the whole integer range. Besides, you never know, maybe your
files will grow over time to over 32767 lines.

So how what to use?

Since the RTL's sLineBreak constant is CRLF on Windows, and you don't
want to split on bare-LF line breaks, you are going to have to parse
the file data manually, not use LoadFromFile() at all.

What are You mean manuaily? How to read the file content to String object not using LoadFromFile() at all?
--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Loading a file with the LoadFromFile() function with a newline
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 30, 2017 4:59 PM   in response to: duf _ in response to: duf _
duf _ wrote:

What are You mean manuaily?

I mean exactly what I said. You are going to have to process the file
data manually. Open the file yourself, read the file data into memory
yourself, and parse it yourself.

How to read the file content to String object not using LoadFromFile()
at all?

For instance, you could read the whole file into a `System::String`
using the System::Classes::TStreamReader::ReadToEnd() or
System::Ioutils::TFile::ReadAllText() method (TStreamReader and TFile
both have methods for reading lines, but they both treat all three
forms of line breaks equally), and then parse that String to extract
CRLF-delimited substrings while ignoring any bare-LF characters.

FYI, please don't ask the same question on multiple forums. It is
considered rude etiquette. You asked the same question on this forum
and on StackOverflow at nearly the same time. I answered your
StackOverflow question for completeness sake, but please don't do it
again in the future. Pick one forum and stick with it, give people
time to reply before you move to another forum.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02