digitalmars.D.learn - Why doesn't this piece of code work?
- SvGaming (18/18) May 15 2022 I want to ask the user to select their USB drive by typing the
- kdevel (20/38) May 15 2022 ```
- SvGaming (9/30) May 15 2022 Strange. It does not for me. I even tried different compilers.
- kdevel (30/39) May 15 2022 Install the `strace` program (I assume you are running Linux) and
- SvGaming (5/11) May 15 2022 I am so confused right now. It works if that code is the main
- =?UTF-8?Q?Ali_=c3=87ehreli?= (12/15) May 15 2022 Could it be that the main() function exits before calling that other
- kdevel (31/34) May 15 2022 Until you post your full programm ideally in a reduced form [1]
- SvGaming (3/11) May 16 2022 I use dmd but I tried befor with another compiler and got the
- SvGaming (3/16) May 16 2022 Here is the repository on GitHub i just made for the code(i know
- SvGaming (3/20) May 16 2022 https://github.com/svgaming234/ezusb
- kdevel (28/30) May 16 2022 In main your program reads an integer:
- =?UTF-8?Q?Ali_=c3=87ehreli?= (5/12) May 16 2022 I learned to read %s as "whatever the type of the argument is" (not
- SvGaming (2/12) May 16 2022 Thanks for the help!
- SvGaming (3/13) May 17 2022 Just tried the solution and it works perfectly!
- SvGaming (4/22) May 15 2022 Just noticed i forgot to put the proper variable name to the
I want to ask the user to select their USB drive by typing the mount location. For some reason that does not work and just skips over the user input part. ```d void writeusb() { writeln("Here is a list of your mounted drives: "); auto mounts = executeShell("cat /proc/mounts | grep media"); writeln(mounts.output); writef("Type the path to your USB drive: "); string cont = readln; writeln(a); writeln("A file selection menu will now appear so you can select an ISO to write."); auto seliso = executeShell("zenity --file-selection"); writeln(seliso.output); } ``` I am clueless as to what I need to do here? Can anyone help?
May 15 2022
On Sunday, 15 May 2022 at 11:10:41 UTC, SvGaming wrote:I want to ask the user to select their USB drive by typing the mount location. For some reason that does not work and just skips over the user input part. ```d void writeusb() { writeln("Here is a list of your mounted drives: "); auto mounts = executeShell("cat /proc/mounts | grep media"); writeln(mounts.output); writef("Type the path to your USB drive: "); string cont = readln; writeln(a); writeln("A file selection menu will now appear so you can select an ISO to write."); auto seliso = executeShell("zenity --file-selection"); writeln(seliso.output); } ``` I am clueless as to what I need to do here? Can anyone help?``` import std.stdio; import std.process; int main () { writeln("Here is a list of your mounted drives: "); auto mounts = executeShell("cat /proc/mounts | grep media"); writeln(mounts.output); writef("Type the path to your USB drive: "); string cont = readln; writeln(cont); // there was no variable named "a" writeln("A file selection menu will now appear so you can select an ISO to write."); // auto seliso = executeShell("zenity --file-selection"); // writeln(seliso.output); return 0; } ``` This code runs as expected.
May 15 2022
On Sunday, 15 May 2022 at 12:13:14 UTC, kdevel wrote:On Sunday, 15 May 2022 at 11:10:41 UTC, SvGaming wrote:Strange. It does not for me. I even tried different compilers. It simply does not ask for user input here where it is supposed to: ```d writef("Type the path to your USB drive: "); string cont = readln; writeln(cont); // there was no variable named "a" ```[...]``` import std.stdio; import std.process; int main () { writeln("Here is a list of your mounted drives: "); auto mounts = executeShell("cat /proc/mounts | grep media"); writeln(mounts.output); writef("Type the path to your USB drive: "); string cont = readln; writeln(cont); // there was no variable named "a" writeln("A file selection menu will now appear so you can select an ISO to write."); // auto seliso = executeShell("zenity --file-selection"); // writeln(seliso.output); return 0; } ``` This code runs as expected.
May 15 2022
On Sunday, 15 May 2022 at 12:19:22 UTC, SvGaming wrote: [...]Install the `strace` program (I assume you are running Linux) and start your program under strace: ``` $ strace -f <name of the executable> ``` Then examine the system calls. On my machine I get this: ``` [...] mmap(NULL, 4194304, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ffff6beb000 read(4, "", 4096) = 0 --- SIGCHLD (Child exited) 0 (0) --- read(4, "", 4096) = 0 wait4(26713, [{WIFEXITED(s) && WEXITSTATUS(s) == 1}], 0, NULL) = 26713 close(4) = 0 munmap(0x7ffff7ff5000, 4096) = 0 write(1, "\n", 1 ) = 1 fstat(0, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 10), ...}) = 0 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7ffff7ff5000 write(1, "Type the path to your USB drive:"..., 33Type the path to your USB drive: ) = 33 read(0, ``` Here the program waits for input.This code runs as expected.Strange. It does not for me. I even tried different compilers. It simply does not ask for user input here where it is supposed to: ```d writef("Type the path to your USB drive: "); string cont = readln; writeln(cont); // there was no variable named "a" ```
May 15 2022
On Sunday, 15 May 2022 at 12:27:45 UTC, kdevel wrote:On Sunday, 15 May 2022 at 12:19:22 UTC, SvGaming wrote: [...]I am so confused right now. It works if that code is the main function. But in the full program that is only one of the functions and is not the main function.[...]Install the `strace` program (I assume you are running Linux) and start your program under strace: [...]
May 15 2022
On 5/15/22 08:27, SvGaming wrote:I am so confused right now. It works if that code is the main function. But in the full program that is only one of the functions and is not the main function.Could it be that the main() function exits before calling that other function? You can sprinkle simple writeln calls to figure out exactly where it goes wrong: void main() { writeln("entered main"); writeln("before calling foo"); foo(); writeln("exiting main"); } Ali
May 15 2022
On Sunday, 15 May 2022 at 15:27:32 UTC, SvGaming wrote: [...]It works if that code is the main function. But in the full program that is only one of the functions and is not the main function.Until you post your full programm ideally in a reduced form [1] everybody who is willing to help must guess wildly what the unposted parts of your program does and how it may cause the read to be seemingly skipped. ``` import std.stdio; import std.process; void foo () { writeln("Here is a list of your mounted drives: "); auto mounts = executeShell("cat /proc/mounts | grep media"); writeln(mounts.output); writef("Type the path to your USB drive: "); string cont = readln; writeln(cont); // there was no variable named "a" writeln("A file selection menu will now appear so you can select an ISO to write."); // auto seliso = executeShell("zenity --file-selection"); // writeln(seliso.output); } int main () { foo; return 0; } ``` Also works as expected. What does `strace` report? And what compiler/version do you use? [1] https://stackoverflow.com/help/minimal-reproducible-example
May 15 2022
On Sunday, 15 May 2022 at 20:06:20 UTC, kdevel wrote:On Sunday, 15 May 2022 at 15:27:32 UTC, SvGaming wrote: [...]I use dmd but I tried befor with another compiler and got the same result. Not sure if it was GDC or LDC.[...]Until you post your full programm ideally in a reduced form [1] everybody who is willing to help must guess wildly what the unposted parts of your program does and how it may cause the read to be seemingly skipped. [...]
May 16 2022
On Monday, 16 May 2022 at 16:40:59 UTC, SvGaming wrote:On Sunday, 15 May 2022 at 20:06:20 UTC, kdevel wrote:Here is the repository on GitHub i just made for the code(i know it is quite messy, I am a begginer in D):On Sunday, 15 May 2022 at 15:27:32 UTC, SvGaming wrote: [...]I use dmd but I tried befor with another compiler and got the same result. Not sure if it was GDC or LDC.[...]Until you post your full programm ideally in a reduced form [1] everybody who is willing to help must guess wildly what the unposted parts of your program does and how it may cause the read to be seemingly skipped. [...]
May 16 2022
On Monday, 16 May 2022 at 16:44:13 UTC, SvGaming wrote:On Monday, 16 May 2022 at 16:40:59 UTC, SvGaming wrote:https://github.com/svgaming234/ezusb forgot to post it in the previous reply, I am kind of stupidOn Sunday, 15 May 2022 at 20:06:20 UTC, kdevel wrote:Here is the repository on GitHub i just made for the code(i know it is quite messy, I am a begginer in D):On Sunday, 15 May 2022 at 15:27:32 UTC, SvGaming wrote: [...]I use dmd but I tried befor with another compiler and got the same result. Not sure if it was GDC or LDC.[...]Until you post your full programm ideally in a reduced form [1] everybody who is willing to help must guess wildly what the unposted parts of your program does and how it may cause the read to be seemingly skipped. [...]
May 16 2022
On Monday, 16 May 2022 at 16:53:15 UTC, SvGaming wrote: [...]https://github.com/svgaming234/ezusb forgot to post it in the previous reply, I am kind of stupidIn main your program reads an integer: ``` int n; writef("Pick an option: "); readf(" %s", &n); ``` but your console/Terminal is line buffered, i.e. you type 1 plus a newline ("\n"). Only after the newline the OS transfers control back to your program. Unfortunately your program does not consume the newline, as ltrace reveals the newline is put back to the input buffer with ungetch. Therefore the next read command ``` string a; a = readln(); writeln(a); ``` consumes that newline from your first input resulting in variable a containing the empty string ending with a newline. Following change fixes the issue: ``` int n; writef("Pick an option: "); readf(" %s\n", &n); // explicitly read the newline ``` I am not sure, why %s fits here. I would have expected a %d format for parsing the integer.
May 16 2022
On 5/16/22 15:25, kdevel wrote:string a; a = readln(); writeln(a);consumes that newline from your first input resulting in variable a containing the empty string ending with a newline.Great catch! I should put this combination in my chapter.I am not sure, why %s fits here. I would have expected a %d format for parsing the integer.I learned to read %s as "whatever the type of the argument is" (not "string"). Ali
May 16 2022
On Monday, 16 May 2022 at 22:25:23 UTC, kdevel wrote:On Monday, 16 May 2022 at 16:53:15 UTC, SvGaming wrote: [...]Thanks for the help![...]In main your program reads an integer: ``` int n; writef("Pick an option: "); readf(" %s", &n); ``` [...]
May 16 2022
On Monday, 16 May 2022 at 22:25:23 UTC, kdevel wrote:On Monday, 16 May 2022 at 16:53:15 UTC, SvGaming wrote: [...]Just tried the solution and it works perfectly! Thanks, Again![...]In main your program reads an integer: ``` int n; writef("Pick an option: "); readf(" %s", &n); ``` [...]
May 17 2022
On Sunday, 15 May 2022 at 11:10:41 UTC, SvGaming wrote:I want to ask the user to select their USB drive by typing the mount location. For some reason that does not work and just skips over the user input part. ```d void writeusb() { writeln("Here is a list of your mounted drives: "); auto mounts = executeShell("cat /proc/mounts | grep media"); writeln(mounts.output); writef("Type the path to your USB drive: "); string cont = readln; writeln(a); writeln("A file selection menu will now appear so you can select an ISO to write."); auto seliso = executeShell("zenity --file-selection"); writeln(seliso.output); } ``` I am clueless as to what I need to do here? Can anyone help?Just noticed i forgot to put the proper variable name to the input. Should not matter though. Also when I change it nothing changes.
May 15 2022