digitalmars.D.learn - Simple file manipulation
- Sam Hu (17/17) May 20 2009 I looked up in D2 in std.stdio,std.file,std.cstream and std.stream and ...
- BLS (8/33) May 20 2009 IN D2 you can use std.file and slurp (cool name, beside)
- Tyro[a.c.edwards] (11/50) May 20 2009 Unfortunately, that will not work. DMD fails with a Stack Overflow
I looked up in D2 in std.stdio,std.file,std.cstream and std.stream and try to find a very simple method which can read from a file once a value other than once an entire row.I just can not find it maybe this idea is wrong.Say how to simply read & write key/value pairs to and from a file like this format: //file "data.dat" Tommy M 22 where the keys are name,gender and age while the values are Tommy,M ,22. I found there is methods that can read from a file once an entire row.But is there a simple method which can read once a value?In C++ one can do like this: #include <iosteam> #include <fstream> using namespace std; ifstream inData; inData.open("data.dat"); inData>>name; inData>>gender; inData>>age; cout<<"Info:"<<endl <<"Name:"<<name<<endl <<"Gender:"<<gender<<endl <<"Age:"<<age<<endl;
May 20 2009
Sam Hu wrote:I looked up in D2 in std.stdio,std.file,std.cstream and std.stream and try to find a very simple method which can read from a file once a value other than once an entire row.I just can not find it maybe this idea is wrong.Say how to simply read & write key/value pairs to and from a file like this format: //file "data.dat" Tommy M 22 where the keys are name,gender and age while the values are Tommy,M ,22. I found there is methods that can read from a file once an entire row.But is there a simple method which can read once a value?In C++ one can do like this: #include <iosteam> #include <fstream> using namespace std; ifstream inData; inData.open("data.dat"); inData>>name; inData>>gender; inData>>age; cout<<"Info:"<<endl <<"Name:"<<name<<endl <<"Gender:"<<gender<<endl <<"Age:"<<age<<endl;IN D2 you can use std.file and slurp (cool name, beside) slurp reads an entire file into an array. // Load file; each line is an string followed by whitespace , another //string followed by whitespace and a int. auto a = slurp!(string, string, int)("data.dat", "%s %s %s"); Now you can go on an play a bit with the new range stuff. (std.range) Enjoy, Björn
May 20 2009
On 5/20/2009 6:19 PM, BLS wrote:Sam Hu wrote:Unfortunately, that will not work. DMD fails with a Stack Overflow whenever upon encountering any of the string types (dstring, string, char[], wstring, etc...) being passed to this template. Works fine or other types as far as I can tell (including arrays). import std.file; void main() { auto a = slurp!(string)("", "%s"); } results in Stack Overflow during compilation.I looked up in D2 in std.stdio,std.file,std.cstream and std.stream and try to find a very simple method which can read from a file once a value other than once an entire row.I just can not find it maybe this idea is wrong.Say how to simply read & write key/value pairs to and from a file like this format: //file "data.dat" Tommy M 22 where the keys are name,gender and age while the values are Tommy,M ,22. I found there is methods that can read from a file once an entire row.But is there a simple method which can read once a value?In C++ one can do like this: #include <iosteam> #include <fstream> using namespace std; ifstream inData; inData.open("data.dat"); inData>>name; inData>>gender; inData>>age; cout<<"Info:"<<endl <<"Name:"<<name<<endl <<"Gender:"<<gender<<endl <<"Age:"<<age<<endl;IN D2 you can use std.file and slurp (cool name, beside) slurp reads an entire file into an array. // Load file; each line is an string followed by whitespace , another //string followed by whitespace and a int. auto a = slurp!(string, string, int)("data.dat", "%s %s %s"); Now you can go on an play a bit with the new range stuff. (std.range) Enjoy, Björn
May 20 2009