Thread overview | ||||||
---|---|---|---|---|---|---|
|
October 02, 2013 Write to file in a function | ||||
---|---|---|---|---|
| ||||
I would like to open a file before I enter a function and be able to write to it while I'm in the function. I'm not having any luck. Shared? Global? Pass a pointer to the funcion? auto fout = File("myfile.csv", "w"); is what I'm currently using. Thanks for any assistance. |
October 02, 2013 Re: Write to file in a function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On Wednesday, 2 October 2013 at 21:44:37 UTC, Paul wrote:
> auto fout = File("myfile.csv", "w"); is what I'm currently using.
You should be able to just pass fout to a function normally:
void writeTo(File fout) {
fout.writeln("info,here,etc");
}
void main() {
auto fout = File("myfile.csv", "w");
writeTo(fout);
}
|
October 02, 2013 Re: Write to file in a function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | On 10/02/2013 02:44 PM, Paul wrote:
> I would like to open a file before I enter a function and be able to
> write to it while I'm in the function. I'm not having any luck.
> Shared? Global? Pass a pointer to the funcion?
>
> auto fout = File("myfile.csv", "w"); is what I'm currently using.
>
> Thanks for any assistance.
import std.stdio;
void foo(File file)
{
file.writeln("my csv\n");
}
void main()
{
auto fout = File("myfile.csv", "w");
foo(fout);
}
Ali
|
October 05, 2013 Re: Write to file in a function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | Wow. I'm really embarrased. But thanks guys.
On Wednesday, 2 October 2013 at 21:57:08 UTC, Ali Çehreli wrote:
> On 10/02/2013 02:44 PM, Paul wrote:
>> I would like to open a file before I enter a function and be able to
>> write to it while I'm in the function. I'm not having any luck.
>> Shared? Global? Pass a pointer to the funcion?
>>
>> auto fout = File("myfile.csv", "w"); is what I'm currently using.
>>
>> Thanks for any assistance.
>
> import std.stdio;
>
> void foo(File file)
> {
> file.writeln("my csv\n");
> }
>
> void main()
> {
> auto fout = File("myfile.csv", "w");
> foo(fout);
> }
>
> Ali
|
Copyright © 1999-2021 by the D Language Foundation