Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changes from Version 1 of ImportFile

Show
Ignore:
Author:
nascent (IP: 67.110.217.136)
Timestamp:
02/05/10 04:10:36 (14 years ago)
Comment:

Page created

Legend:

Unmodified
Added
Removed
Modified
  • ImportFile

    v0 v1  
     1= Reading Files at Compile Time = 
     2 
     3== Description == 
     4 
     5D lets you do a lot code execution during compile time, also known as Compile Time Function Evaluation (CTFE). Sometimes the information you want to work with at compile time is located in a file, you can access the content by using import("filename"); which returns a character array. 
     6 
     7== Example == 
     8 
     9File: ctfi.txt 
     10{{{ 
     11Hello World 
     12}}} 
     13 
     14File: ctfi.d 
     15{{{ 
     16#!d 
     17import std.stdio; 
     18 
     19void main() { 
     20        const foo = import("ctfi.txt"); 
     21 
     22        writefln(foo); 
     23} 
     24}}} 
     25 
     26If you try to compile this with $ dmd ctfi.d you will receive the error 
     27 
     28{{{ctfi.d(6): Error: need -Jpath switch to import text file ctfi.txt}}} 
     29 
     30This is the path for file imports that are not libraries. Use the following for compilation: 
     31 
     32Compile: 
     33{{{ 
     34dmd -J. ctfi.d 
     35}}}