File Input

k-five via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 7 09:40:50 PDT 2017


On Sunday, 7 May 2017 at 15:59:25 UTC, JV wrote:
> 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:

> 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();

--------------------------------------------------------------

You have the right for confusing :) there is many read and write 
names. But I assumed you are familiar with [Type] and [Object] 
concept.

in:
auto output_file_stream = File( "file.txt", "w" );

auto = File              == A type
File( "file.txt", "w" ); == Constructor

So this type has its own property, like read for "r" mode and 
write for "w" mode.

So you should use output_file_stream.write(), not readf or so on.

Still I am very new in D, but this is the same concept in other 
language like C++

in C++:
#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char **argv)
{
	
	std::ofstream ofs( "file.txt" );
	std::string line = "This is the first line";
         // write is a method in class ofstream
	ofs.write( &*line.begin(), line.length() );
	ofs.close();
}


More information about the Digitalmars-d-learn mailing list