| Thread overview | |||||
|---|---|---|---|---|---|
|
May 28, 2017 Input interrupt | ||||
|---|---|---|---|---|
| ||||
Hello, I just wrote a mock-up of Unix's $cat. However unlike the actual $cat, when input interrupt (Cntrl+D) is pressed the following program does not stop.
So I tried using C's EOF but the types aren't compatible since EOF is probably aliased to -1
//...
if (args.length < 2)
{
string input = readln();
while (input != EOF)
{
input.write();
}
}
//...
Tips?
Here is the full program:
import std.stdio;
import std.file;
void main(string[] args)
{
if (args.length < 2)
{
string input = readln();
while (input != EOF)
{
input.write();
}
}
else
{
foreach (path; args[1..$])
{
auto current_file = File(path, "r");
while (!(current_file.eof()))
{
current_file.readln().write();
}
current_file.close();
}
}
}
| ||||
May 28, 2017 Re: Input interrupt | ||||
|---|---|---|---|---|
| ||||
Posted in reply to helxi | On Sunday, 28 May 2017 at 22:07:12 UTC, helxi wrote: > So I tried using C's EOF but the types aren't compatible since EOF is probably aliased to -1 The readln docs for D say it returns null on end of file. The example given is: import std.stdio; void main() { string line; while ((line = readln()) !is null) write(line); } http://dpldocs.info/experimental-docs/std.stdio.readln.1.html I would try that. | |||
May 28, 2017 Re: Input interrupt | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Sunday, 28 May 2017 at 22:14:46 UTC, Adam D. Ruppe wrote:
> On Sunday, 28 May 2017 at 22:07:12 UTC, helxi wrote:
>> So I tried using C's EOF but the types aren't compatible since EOF is probably aliased to -1
>
>
> The readln docs for D say it returns null on end of file. The example given is:
>
> import std.stdio;
>
> void main()
> {
> string line;
> while ((line = readln()) !is null)
> write(line);
> }
>
> http://dpldocs.info/experimental-docs/std.stdio.readln.1.html
>
> I would try that.
Oh yes, fantastic,
I also seem to have forgotten EOF was meant to compare chars, not strings.
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply