How is string from "..." different from string from a file ?
Timon Gehr
timon.gehr at gmx.ch
Mon Dec 12 07:24:58 PST 2011
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
More information about the Digitalmars-d-learn
mailing list