Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2024/25/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Include the content of a text file

Let's say we have a text file and we want to include its content in a string during compilation.

$ cat lorem.txt
Lorem ipsum dolor,
molestiae quas vel
numquam blanditiis.
(1) embed a text file's content
package templates

// Go code

import _ "embed"

//go:embed lorem.txt
var LoremIpsum string
import std.stdio;
import std.string;

static immutable string LOREM = import("lorem.txt").strip();

void main()
{
    writeln(LOREM);
}

The compilation is a bit tricky:

$ dmd main.d
main.d(6): Error: need `-J` switch to import text file `lorem.txt`
static immutable string LOREM = import("lorem.txt").strip();
                                ^
$ dmd -J. main.d

Why -J is needed:

  • D's import("file.txt") is secure by default. It won't read arbitrary files unless explicitly allowed.
  • -J tells the compiler where to look for imported files. In my case, lorem.txt is next to main.d in the current directory (.)

Move the EXE to a different directory and execute it there:

$ cat lorem.txt
cat: lorem.txt: No such file or directory

$ ./main
Lorem ipsum dolor,
molestiae quas vel
numquam blanditiis.
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 July 02, 12:50