Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
September 06, 2018 file io | ||||
---|---|---|---|---|
| ||||
how to read a file line by line in D |
September 06, 2018 Re: file io | ||||
---|---|---|---|---|
| ||||
Posted in reply to hridyansh thakur | On Thursday, 6 September 2018 at 16:13:42 UTC, hridyansh thakur wrote: > how to read a file line by line in D std.stdio.File.byLine() Refer the doc here: https://dlang.org/library/std/stdio/file.by_line.html An example from the doc: ``` import std.algorithm, std.stdio, std.string; // Count words in a file using ranges. void main() { auto file = File("file.txt"); // Open for reading const wordCount = file.byLine() // Read lines .map!split // Split into words .map!(a => a.length) // Count words per line .sum(); // Total word count writeln(wordCount); } ``` |
September 07, 2018 Re: file io | ||||
---|---|---|---|---|
| ||||
Posted in reply to Arun Chandrasekaran | On 07/09/2018 4:17 AM, Arun Chandrasekaran wrote:
> On Thursday, 6 September 2018 at 16:13:42 UTC, hridyansh thakur wrote:
>> how to read a file line by line in D
>
> std.stdio.File.byLine()
>
> Refer the doc here: https://dlang.org/library/std/stdio/file.by_line.html
>
> An example from the doc:
>
> ```
> import std.algorithm, std.stdio, std.string;
> // Count words in a file using ranges.
> void main()
> {
> auto file = File("file.txt"); // Open for reading
> const wordCount = file.byLine() // Read lines
> .map!split // Split into words
> .map!(a => a.length) // Count words per line
> .sum(); // Total word count
> writeln(wordCount);
> }
> ```
Ranges will be far too advanced of a topic to bring up at this stage.
So something a little more conventional might be a better option:
---
import std.file : readText;
import std.array : split;
import std.string : strip;
string text = readText("file.txt");
string[] onlyWords = text.split(" ");
uint countWords;
foreach(ref word; onlyWords) {
word = word.strip();
if (word.length > 0)
countWords++;
}
---
|
September 06, 2018 Re: file io | ||||
---|---|---|---|---|
| ||||
Posted in reply to rikki cattermole | On 9/6/18 1:07 PM, rikki cattermole wrote:
> On 07/09/2018 4:17 AM, Arun Chandrasekaran wrote:
>> On Thursday, 6 September 2018 at 16:13:42 UTC, hridyansh thakur wrote:
>>> how to read a file line by line in D
>>
>> std.stdio.File.byLine()
>>
>> Refer the doc here: https://dlang.org/library/std/stdio/file.by_line.html
>>
>> An example from the doc:
>>
>> ```
>> import std.algorithm, std.stdio, std.string;
>> // Count words in a file using ranges.
>> void main()
>> {
>> auto file = File("file.txt"); // Open for reading
>> const wordCount = file.byLine() // Read lines
>> .map!split // Split into words
>> .map!(a => a.length) // Count words per line
>> .sum(); // Total word count
>> writeln(wordCount);
>> }
>> ```
>
> Ranges will be far too advanced of a topic to bring up at this stage.
>
> So something a little more conventional might be a better option:
>
> ---
> import std.file : readText;
> import std.array : split;
> import std.string : strip;
>
> string text = readText("file.txt");
> string[] onlyWords = text.split(" ");
>
> uint countWords;
> foreach(ref word; onlyWords) {
> word = word.strip();
> if (word.length > 0)
> countWords++;
> }
> ---
Ugh, don't do that, it will read the unknown-length file into RAM all at once.
foreach(word; File("file.txt").byLine)
{
word = word.strip();
if(word.length > 0) countWords++;
}
That will buffer one line at a time and achieve the same results.
-Steve
|
September 06, 2018 Re: file io | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 9/6/18 2:30 PM, Steven Schveighoffer wrote:
> On 9/6/18 1:07 PM, rikki cattermole wrote:
>> On 07/09/2018 4:17 AM, Arun Chandrasekaran wrote:
>>> On Thursday, 6 September 2018 at 16:13:42 UTC, hridyansh thakur wrote:
>>>> how to read a file line by line in D
>>>
>>> std.stdio.File.byLine()
>>>
>>> Refer the doc here: https://dlang.org/library/std/stdio/file.by_line.html
>>>
>>> An example from the doc:
>>>
>>> ```
>>> import std.algorithm, std.stdio, std.string;
>>> // Count words in a file using ranges.
>>> void main()
>>> {
>>> auto file = File("file.txt"); // Open for reading
>>> const wordCount = file.byLine() // Read lines
>>> .map!split // Split into words
>>> .map!(a => a.length) // Count words per line
>>> .sum(); // Total word count
>>> writeln(wordCount);
>>> }
>>> ```
>>
>> Ranges will be far too advanced of a topic to bring up at this stage.
>>
>> So something a little more conventional might be a better option:
>>
>> ---
>> import std.file : readText;
>> import std.array : split;
>> import std.string : strip;
>>
>> string text = readText("file.txt");
>> string[] onlyWords = text.split(" ");
>>
>> uint countWords;
>> foreach(ref word; onlyWords) {
>> word = word.strip();
>> if (word.length > 0)
>> countWords++;
>> }
>> ---
>
> Ugh, don't do that, it will read the unknown-length file into RAM all at once.
>
> foreach(word; File("file.txt").byLine)
> {
> word = word.strip();
> if(word.length > 0) countWords++;
> }
>
> That will buffer one line at a time and achieve the same results.
ugh, I didn't think this through. This works:
foreach(line; File("file.txt").byLine)
{
foreach(word; line.split(" "))
{
word = word.strip();
if(word.length > 0) countWords++;
}
}
-Steve
|
Copyright © 1999-2021 by the D Language Foundation