Noob questions

0x10001 changdoelee at gmail.com
Wed Apr 24 22:44:17 UTC 2019


Hello, i've hit some roadblocks while trying to learn D, i'll try 
to explain each of them below :

1: Why does using readf in a function causes readln in following 
functions to "skip"(or do as if i had pressed enter?)
Here's an example code(excuse me if it doesn't appear formatted) :

import std.stdio;
import std.string;

void main() {
	ubyte menuint;
	writeln("test");
	readf(" %s", &menuint);
	if (menuint == 1) {
		foo();
	} else {
		bar();
	}
}

void foo() {
	string a = strip(readln());
	writeln(a);
}

void bar() {
	string b = strip(readln());
	writeln(b);
}
The code after the readf function just prints two newlines 
without even asking for input.


2: Why can i do whatever to strings that represents filenames but 
using stdin for filenames with (strip(readln()) always causes a 
"No such file or directory" error?

I've tried the same with chop(readln()), the test file is named 
"TEST.TXT" and while using ''' File file = File("TEST.TXT", "r") 
''' works, but doing the same with a variable that contains the 
same exact filename obtained with <<std.string.strip(readln())>> 
or <<std.string.chop(readln())>> always causes a "No such file or 
directory" error. I've tried to make it print the read filename 
before and it visually appears to be the exact same.
I'm on Windows in case this is a compatibility problem and it 
works as expected on everything else.

3: How can files be accessed once opened with read access?(as 
described in Ali Çehreli's Programming in D, chapter 19) Am i 
right when assuming they are just void[] arrays that can be 
"casted" to another array type(if appropriate within current 
context) resulting in what's called a buffer? I'm asking because 
i've been trying to make a simple program with std.zlib that 
uncompresses/compresses all zlib files that follow a certain name 
pattern, only problem is that apparently the std.file.write 
function doesn't pass compilation because it can't use anything 
that has "File" in its type, what am i supposed to do then?(the 
exact error is "template std.file.write cannot deduce function 
from argument types !()(File, void[])", i got the same error with 
the std.stdio.write function)

Here's the code in question that i have trouble with :
		if (exists(filename)) {
			writeln("file found!");
			File efile = File(filename, "r");
			File rawfile = File(filename ~ ".out", "w");
			void[] efile_buf1 = lire(filename); // alias for std.file.read
			ubyte[] efile_buf2 = cast(ubyte[]) efile_buf1;
			rawfile.écrire(uncompress(efile_buf2)); // alias for 
std.file.write
			succ(namecounter);


Thanks to everyone in advance.


More information about the Digitalmars-d-learn mailing list