www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - File Input

reply JV <jrpv100997 yahoo.com> writes:
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.
May 07
next sibling parent reply k-five <vhsu30 yahoo.com> writes:
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
May 07
parent reply JV <jrpv100997 yahoo.com> writes:
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();
May 07
parent reply k-five <vhsu30 yahoo.com> writes:
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(); }
May 07
parent reply JV <jrpv100997 yahoo.com> writes:
On Sunday, 7 May 2017 at 16:40:50 UTC, k-five wrote:
 On Sunday, 7 May 2017 at 15:59:25 UTC, JV wrote:
 [...]
         [...]
-------------------------------------------------------------- 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(); }
Yeah i understand it very much like the other language like C/C++ and python.. since i'm self studying D language ..though i learn faster when there is a sample code i don't know if it is rude but can i ask if you can give me a sample code for it? a code for asking the user to enter something and then store it in a .txt file? Thank you
May 08
parent reply k-five <vhsu30 yahoo.com> writes:
On Monday, 8 May 2017 at 08:54:50 UTC, JV wrote:
 On Sunday, 7 May 2017 at 16:40:50 UTC, k-five wrote:
 On Sunday, 7 May 2017 at 15:59:25 UTC, JV wrote:
--------------------------------------------------- Do not worry. Your request is not rude. I give you a better tool. I finished to collect some examples in D and in a few days I will share it in my githup https://github.com/k-five/ and you can use it. I tested all examples and even put the output of each, beside the example. here is the list of them: ├── 01_overview ├── 02_environment ├── 03_basic_syntax ├── 04_variable ├── 05_data-type ├── 06_enums ├── 07_literals ├── 08_operators ├── 09_loops ├── 10_decision ├── 11_functons ├── 12_characters ├── 13_strings ├── 14_array ├── 15_associative_array ├── 16_pointers ├── 17_tuple ├── 18_struct ├── 19_union ├── 20_range ├── 21_alias ├── 22_mixin ├── 23_module ├── 24_template ├── 25_immutable_type ├── 26_file_io ├── 27_thread ├── 28_exception ├── 29_contract_programming ├── 30_conditional_compilation ├── 31_classes ├── 32_inheritance ├── 33_overloading ├── 34_encapsulation ├── 35_interface └── 36_abstract plus some examples for D feature and how to install d-mode on emacs editor I got familiar with D for two weeks, so I am a beginner too. You will see these examples ( almost 190 ) until Friday on my githup.
May 08
parent reply JV <jrpv100997 yahoo.com> writes:
On Monday, 8 May 2017 at 09:26:48 UTC, k-five wrote:
 On Monday, 8 May 2017 at 08:54:50 UTC, JV wrote:
 [...]
--------------------------------------------------- Do not worry. Your request is not rude. I give you a better tool. I finished to collect some examples in D and in a few days I will share it in my githup https://github.com/k-five/ and you can use it. I tested all examples and even put the output of each, beside the example. [...]
Thank you and i hope to see more of you collection about D's filestream its very helpful
May 08
parent reply k-five <vhsu30 yahoo.com> writes:
On Monday, 8 May 2017 at 10:22:53 UTC, JV wrote:
 On Monday, 8 May 2017 at 09:26:48 UTC, k-five wrote:
 On Monday, 8 May 2017 at 08:54:50 UTC, JV wrote:
--------------------------------------------------- If I continue to learn D I will do but there is no guarantee and it got ready :) https://github.com/k-five/D-By-Example if you have git, download them: git clone https://github.com/k-five/D-By-Example.git
May 08
parent reply JV <jrpv100997 yahoo.com> writes:
On Monday, 8 May 2017 at 10:34:42 UTC, k-five wrote:
 On Monday, 8 May 2017 at 10:22:53 UTC, JV wrote:
 On Monday, 8 May 2017 at 09:26:48 UTC, k-five wrote:
 On Monday, 8 May 2017 at 08:54:50 UTC, JV wrote:
--------------------------------------------------- If I continue to learn D I will do but there is no guarantee and it got ready :) https://github.com/k-five/D-By-Example if you have git, download them: git clone https://github.com/k-five/D-By-Example.git
Hey i'm not sure if i should create a new post for this but how should i fix this it doesn't pause and store but just keeps reading string studNum; readf("%s",&studNum); write(studNum);
May 13
next sibling parent k-five <vhsu30 yahoo.com> writes:
On Sunday, 14 May 2017 at 04:15:02 UTC, JV wrote:
 Hey i'm not sure if i should create a new post for this but
 how should i fix this it doesn't pause and store but just keeps 
 reading

         string studNum;

         readf("%s",&studNum);
         write(studNum);
Can you say exactly what you need? It is better to be familiar with C and File in C, then using File in D However, do not hesitate about asking, just feel free and ask
May 14
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 05/13/2017 09:15 PM, JV wrote:
 it doesn't pause and store but just keeps reading

         string studNum;

         readf("%s",&studNum);
         write(studNum);
That's the normal behavior for reading into strings. If you want to read to the end of the line, try this: import std.stdio; import std.string; void main() { write("What is your name? "); string name = readln.strip; writeln("Hello ", name, "!"); } (It's the same as strip(readln()).) Here is more information about readln() and strip() as well as formattedRead(), which can be more convenient: http://ddili.org/ders/d.en/strings.html Ali
May 14
prev sibling parent Suliman <evermind live.ru> writes:
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.
http://nomad.so/2015/09/working-with-files-in-the-d-programming-language/
May 07