| Thread overview | |||||||||
|---|---|---|---|---|---|---|---|---|---|
|
March 15, 2016 write to file array by lines | ||||
|---|---|---|---|---|
| ||||
I have got: string [] total_content; I am appending to it data on every iteration. total_content ~= somedata File file = File(`D:\code\2vlad\result.txt`, "a+"); file.write(total_content); I need to write it's to file by lines. Like: somedataline1 somedataline2 somedataline3 I tried to do like: total_content ~= somedata ~ "\n" but in result I am getting all data in one line: somedataline1 "\n" somedataline2 "\n" somedataline3 "\n" what I am doing wrong? I know about split, but how it can be called on writing time? | ||||
March 15, 2016 Re: write to file array by lines | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Tuesday, 15 March 2016 at 10:58:16 UTC, Suliman wrote:
> I have got:
> string [] total_content;
>
> I am appending to it data on every iteration.
>
> total_content ~= somedata
>
> File file = File(`D:\code\2vlad\result.txt`, "a+");
> file.write(total_content);
>
> I need to write it's to file by lines. Like:
> somedataline1
> somedataline2
> somedataline3
>
> I tried to do like:
> total_content ~= somedata ~ "\n"
>
> but in result I am getting all data in one line:
> somedataline1 "\n" somedataline2 "\n" somedataline3 "\n"
>
> what I am doing wrong? I know about split, but how it can be called on writing time?
Windows CRLF line endings, perhaps?
| |||
March 15, 2016 Re: write to file array by lines | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Tuesday, 15 March 2016 at 10:58:16 UTC, Suliman wrote:
> I have got:
> string [] total_content;
>
> I am appending to it data on every iteration.
>
> total_content ~= somedata
>
> File file = File(`D:\code\2vlad\result.txt`, "a+");
> file.write(total_content);
>
> I need to write it's to file by lines. Like:
> somedataline1
> somedataline2
> somedataline3
>
> I tried to do like:
> total_content ~= somedata ~ "\n"
>
> but in result I am getting all data in one line:
> somedataline1 "\n" somedataline2 "\n" somedataline3 "\n"
>
> what I am doing wrong? I know about split, but how it can be called on writing time?
You're on windows, is newline not '\r\n' ? (I'm guessing here!)
| |||
March 15, 2016 Re: write to file array by lines | ||||
|---|---|---|---|---|
| ||||
Posted in reply to wobbles | I created better example to show. string [] myarr = ["foo", "bar", "baz"]; myarr ~= "new"; File file = File(`result.txt`, "w"); file.write(myarr); is any way to write myarr to file in byLine mode | |||
March 15, 2016 Re: write to file array by lines | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Tuesday, 15 March 2016 at 12:55:17 UTC, Suliman wrote:
> I created better example to show.
>
> string [] myarr = ["foo", "bar", "baz"];
> myarr ~= "new";
> File file = File(`result.txt`, "w");
> file.write(myarr);
>
> is any way to write myarr to file in byLine mode
myarr
.each!(line => file.writeln(line));
| |||
March 15, 2016 Re: write to file array by lines | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Tuesday, 15 March 2016 at 12:55:17 UTC, Suliman wrote: > I created better example to show. > > string [] myarr = ["foo", "bar", "baz"]; > myarr ~= "new"; > File file = File(`result.txt`, "w"); > file.write(myarr); > > is any way to write myarr to file in byLine mode void main(){ import std.stdio; import std.ascii; import std.algorithm; string[] myArr = ["foo", "bar", "baz"]; myArr ~= "new"; writeln("=== each! ==="); myArr.each!(a => writeln(a)); writeln("=== Using format ==="); writefln("%-(%s\n%)", myArr); writeln("=== Using joiner ==="); myArr.joiner(std.ascii.newline).writeln; } Output: === each! === foo bar baz new === Using format === foo bar baz new === Using joiner === foo bar baz new | |||
March 15, 2016 Re: write to file array by lines | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Tuesday, 15 March 2016 at 10:58:16 UTC, Suliman wrote: > I have got: > string [] total_content; > > I am appending to it data on every iteration. > > total_content ~= somedata > > File file = File(`D:\code\2vlad\result.txt`, "a+"); > file.write(total_content); > > I need to write it's to file by lines. Like: > somedataline1 > somedataline2 > somedataline3 > > I tried to do like: > total_content ~= somedata ~ "\n" > > but in result I am getting all data in one line: > somedataline1 "\n" somedataline2 "\n" somedataline3 "\n" > > what I am doing wrong? I know about split, but how it can be called on writing time? http://forum.dlang.org/group/learn would be more appropriate for this sort of question. | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply