www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - error with reading file name

reply "Suliman" <evermind live.ru> writes:
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
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
prev sibling next sibling parent "Suliman" <evermind live.ru> writes:
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
prev sibling parent reply ollie <ollie home.net> writes:
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
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 12/06/2012 10:39 PM, ollie wrote:
 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.

Good call.
 	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