digitalmars.D.learn - error with reading file name
- Suliman (30/30) Dec 06 2012 I am trying to create simple app that would read user input and
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (17/45) Dec 06 2012 The exception is thrown here:
- bearophile (6/10) Dec 06 2012 Try to declutter your code as much as possible, then print the
- Suliman (16/16) Dec 06 2012 I had put next try-catch block yo my code
- ollie (20/25) Dec 06 2012 After a call to readln, the string returned has termination characters
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (7/18) Dec 06 2012 Or with std.string.chomp:
I am trying to create simple app that would read user input and open file with such name, but every time when I run it's crash with error "std.file.FileException std\file.d(294): \1.txt" import std.stdio; import std.string; import std.file; void main() { string getfilename() { auto name = readln(); writeln(name); if (exists(name)) { writeln("exist"); } else writeln("not exist"); return name; } void readfile(string name) { auto filearray = read(name); writeln(name); writeln(filearray); } readfile(getfilename()); }
Dec 06 2012
On 12/06/2012 07:52 AM, Suliman wrote:I am trying to create simple app that would read user input and open file with such name, but every time when I run it's crash with error "std.file.FileException std\file.d(294): \1.txt" import std.stdio; import std.string; import std.file; void main() { string getfilename() { auto name = readln(); writeln(name); if (exists(name)) { writeln("exist"); } else writeln("not exist"); return name; } void readfile(string name) { auto filearray = read(name); writeln(name); writeln(filearray); } readfile(getfilename()); }The exception is thrown here: version(Windows) { alias TypeTuple!(GENERIC_READ, FILE_SHARE_READ, (SECURITY_ATTRIBUTES*).init, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, HANDLE.init) defaults; auto h = CreateFileW(std.utf.toUTF16z(name), defaults); cenforce(h != INVALID_HANDLE_VALUE, name); // <-- HERE It seems to be related to a Unicode encoding issue, possibly the encoding that the console is using. (I am assuming that getfilename() reads from the console.) It could be about file access rights as well. Ali
Dec 06 2012
Suliman:I am trying to create simple app that would read user input and open file with such name, but every time when I run it's crash with error "std.file.FileException std\file.d(294): \1.txt"Try to declutter your code as much as possible, then print the file name before trying to call File, and take a look at what's inside that string. This is not a solution, but it's a start. Bye, bearophile
Dec 06 2012
I had put next try-catch block yo my code void readfile(string name) { try { auto filearray = read(name); writeln(name); writeln(filearray); } catch (Exception e) { writeln("CATHCHED %s", e.msg); } } Am I right with it's place? I get to console only some crap.
Dec 06 2012
On Thu, 06 Dec 2012 16:52:20 +0100, Suliman wrote:I am trying to create simple app that would read user input and open file with such name, but every time when I run it's crash with error "std.file.FileException std\file.d(294): \1.txt"After a call to readln, the string returned has termination characters that need to be stripped off. import std.stdio; import std.file; void main() { string name = readln(); while(name[$-1] == '\x0a' || name[$-1] == '\x0d') name.length -= 1; if(name.exists) writeln(name, " exists!"); else { writeln(name, " doesn't exists!"); return; } auto filearray = cast(char[]) read(name); writeln("\n", filearray); }
Dec 06 2012
On 12/06/2012 10:39 PM, ollie wrote:On Thu, 06 Dec 2012 16:52:20 +0100, Suliman wrote:Good call.I am trying to create simple app that would read user input and open file with such name, but every time when I run it's crash with error "std.file.FileException std\file.d(294): \1.txt"After a call to readln, the string returned has termination characters that need to be stripped off.string name = readln(); while(name[$-1] == '\x0a' || name[$-1] == '\x0d') name.length -= 1;Or with std.string.chomp: string name = chomp(readln()); Ali -- D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
Dec 06 2012