File Input

JV via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 7 08:59:25 PDT 2017


On Sunday, 7 May 2017 at 15:16:58 UTC, k-five wrote:
> On Sunday, 7 May 2017 at 13:57:47 UTC, JV wrote:
>> Hi guys
>>
>> I'd like to know how to get an input from the user to be 
>> stored in a .txt file using import std.file and is it possible 
>> to directly write in a .txt file without using a variable to 
>> store the user input?
>>
>> Thanks for the answer in advance my mind is kinda jumbled 
>> about this since im new to this language.
>
> First of all see here:
> https://dlang.org/phobos/std_stdio.html#.File
>
> also:
>
> import std.stdio; // for File
>
> void main(){
> 	
> 	// an output file with name file.txt
> 	// w for writing
> 	auto ofs = File( "file.txt", "w" );
>
>         // output file stream:
> 	ofs.write( stdin.readln() ); // get a line from console
> 	ofs.close();
> }
>
>
> cat file.txt:
> This is the first line.
>
>
> and for std.file:
> https://dlang.org/phobos/std_file.html



I'm kinda getting it but how do i write the stored user 
input(string) varaible into a .txt??im getting confused since D 
has so many read and write

  ->sample below
         string num;
         auto attendance= File("studAttendance.txt","a+");

         writeln("Add Student Attendance");
         readf("%s ",&num);//im not sure if this is correct but 
assuming it works
                           //how do i write what is stored in num 
in the studAttendance.txt
                           //file??

         attendance.close();


More information about the Digitalmars-d-learn mailing list