digitalmars.D.learn - File Input
- JV (7/7) May 07 2017 Hi guys
- k-five (18/25) May 07 2017 First of all see here:
- JV (14/41) May 07 2017 I'm kinda getting it but how do i write the stored user
- k-five (27/42) May 07 2017 --------------------------------------------------------------
- JV (10/40) May 08 2017 Yeah i understand it very much like the other language like C/C++
- k-five (51/53) May 08 2017 ---------------------------------------------------
- JV (3/13) May 08 2017 Thank you and i hope to see more of you collection about D's
- k-five (7/9) May 08 2017 ---------------------------------------------------
- JV (7/16) May 13 2017 Hey i'm not sure if i should create a new post for this but
- k-five (5/11) May 14 2017 Can you say exactly what you need?
- =?UTF-8?Q?Ali_=c3=87ehreli?= (15/19) May 14 2017 That's the normal behavior for reading into strings. If you want to read...
- Suliman (2/9) May 07 2017 http://nomad.so/2015/09/working-with-files-in-the-d-programming-language...
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 2017
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 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: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();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 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(); }
May 07 2017
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: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[...][...]-------------------------------------------------------------- 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 08 2017
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:--------------------------------------------------- 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.On Sunday, 7 May 2017 at 15:59:25 UTC, JV wrote:
May 08 2017
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:Thank you and i hope to see more of you collection about D's filestream its very helpful--------------------------------------------------- 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. [...][...]
May 08 2017
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:--------------------------------------------------- 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.gitOn Monday, 8 May 2017 at 08:54:50 UTC, JV wrote:
May 08 2017
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: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);On Monday, 8 May 2017 at 09:26:48 UTC, k-five 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.gitOn Monday, 8 May 2017 at 08:54:50 UTC, JV wrote:
May 13 2017
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 2017
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 2017
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 2017