www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How is string from "..." different from string from a file ?

reply ParticlePeter <ParticlePeter gmx.de> writes:
Hi,

I have a hard time reading in a string from a file. I don't get any compile
time or run time errors, but my application does not work reliably when I read
a string from a file. But when I define the same string within my code,
everything runs perfect, allways.
The string I want to use is an OpenGL Shader, but the problem is not to be
related to OpenGL as far as a I see. 
Are there some format strings which I need to get rid of, and how ?

I tried:
import std.file ;
string fragString = readText( "Shader.vert" ) ;

import std.file , std.utf ;
string fragString = toUTF8( readText( "Shader.vert" ) ) ;

import std.stdio ;
string text = "" ;
auto file = File( "Shader.vert" ) ;
foreach( line ; file.byLine() )  string ~= strip( to!( string )( line ) ) ;

What else could I try ?

Cheers, ParticlePeter !
Dec 12 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 12/12/2011 03:35 PM, ParticlePeter wrote:
 Hi,

 I have a hard time reading in a string from a file. I don't get any compile
time or run time errors, but my application does not work reliably when I read
a string from a file. But when I define the same string within my code,
everything runs perfect, allways.
 The string I want to use is an OpenGL Shader, but the problem is not to be
related to OpenGL as far as a I see.
 Are there some format strings which I need to get rid of, and how ?

 I tried:
 import std.file ;
 string fragString = readText( "Shader.vert" ) ;

 import std.file , std.utf ;
 string fragString = toUTF8( readText( "Shader.vert" ) ) ;

 import std.stdio ;
 string text = "" ;
 auto file = File( "Shader.vert" ) ;
 foreach( line ; file.byLine() )  string ~= strip( to!( string )( line ) ) ;

 What else could I try ?

 Cheers, ParticlePeter !

OpenGL probably wants a zero-terminated string. It works if you add the code as a literal because string literals are zero-terminated. string fragString = readText( "Shader.vert" ) ~ '\0'; Alternatively, you can embed the file in you executable: immutable string fragString = import( "Shader.vert" ); // read at compile time
Dec 12 2011
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Timon Gehr:

 string fragString = readText( "Shader.vert" ) ~ '\0';

I think using toStringz is more self-documenting. Bye, bearophile
Dec 12 2011
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 12/12/2011 06:37 PM, bearophile wrote:
 Timon Gehr:

 string fragString = readText( "Shader.vert" ) ~ '\0';

I think using toStringz is more self-documenting. Bye, bearophile

There is nothing more self-documenting than actually appending the zero. Claiming toStringz is better in that regard is like saying a+1 is less self-documenting than doAddo(a) ;) There might be other benefits of using toStringz though, (for example, it won't add the zero if it is already there, but that does not apply here).
Dec 12 2011
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 12 Dec 2011 12:59:11 -0500, Timon Gehr <timon.gehr gmx.ch> wrote:

 On 12/12/2011 06:37 PM, bearophile wrote:
 Timon Gehr:

 string fragString = readText( "Shader.vert" ) ~ '\0';

I think using toStringz is more self-documenting. Bye, bearophile

There is nothing more self-documenting than actually appending the zero. Claiming toStringz is better in that regard is like saying a+1 is less self-documenting than doAddo(a) ;) There might be other benefits of using toStringz though, (for example, it won't add the zero if it is already there, but that does not apply here).

x ~ y *always* makes a copy of x, whereas toStringz(x) will (should?) use append (which could potentially save another heap allocation). However, I'm not sure what kind of state the result of readText is in. -Steve
Dec 12 2011
prev sibling parent ParticlePeter <ParticlePeter gmx.de> writes:
Thank you very much, you made my day, that was it :-)

Cheers, ParticlePeter !

 OpenGL probably wants a zero-terminated string. It works if you add the 
 code as a literal because string literals are zero-terminated.
 
 string fragString = readText( "Shader.vert" ) ~ '\0';
 
 Alternatively, you can embed the file in you executable:
 
 immutable string fragString = import( "Shader.vert" ); // read at 
 compile time

Dec 12 2011