| Thread overview | ||||||
|---|---|---|---|---|---|---|
|
September 24, 2014 Recursive function call | ||||
|---|---|---|---|---|
| ||||
string getFileName()
{
//чтобы было проще обрабатываемый файл будем хранить рядом с бинариком
string filename = chomp(readln());
string path = getcwd();
writeln((path ~ "\\" ~ filename));
if (exists(path ~ "\\" ~ filename))
return (path ~ "\\" ~ filename);
else
{
writeln("File do not exists. Please try again");
getFilename(); //I need something similar, to call function again.
}
}
In case of error, how I can call function getFileName again?
| ||||
September 24, 2014 Re: Recursive function call | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman | How about using a loop?
string getFileName() {
while(true) {
string filename = chomp(readln());
string path = getcwd();
writeln((path ~ "\\" ~ filename));
if (exists(path ~ "\\" ~ filename))
return (path ~ "\\" ~ filename);
writeln("File do not exists. Please try again");
}
}
| |||
September 24, 2014 Re: Recursive function call | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Wednesday, 24 September 2014 at 10:57:27 UTC, Suliman wrote: > string getFileName() > { > //чтобы было проще обрабатываемый файл будем хранить рядом с бинариком > string filename = chomp(readln()); > string path = getcwd(); > writeln((path ~ "\\" ~ filename)); > if (exists(path ~ "\\" ~ filename)) > return (path ~ "\\" ~ filename); Don't do `path ~ "\\" ~ filename`. Instead, use "std.path.buildPath". It's more portable, faster, and convenient. Also, you should store the result in a named variable, or you'll reallocate a new string everytime. > else > { > writeln("File do not exists. Please try again"); > getFilename(); //I need something similar, to call function again. > } > > } > > > In case of error, how I can call function getFileName again? Any kind of looping control structure would work. I'd advocate the while(1) loop with a break or return: while(1) { string filename = chomp(readln()); string path = getcwd(); string fullPath = buildPath(path, filename); if (exists(fullPath)) return fullPath; writeln("File does not exists. Please try again"); } | |||
September 24, 2014 Re: Recursive function call | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Suliman | On Wednesday, 24 September 2014 at 10:57:27 UTC, Suliman wrote: > string getFileName() > { [...] > getFilename(); //I need something similar, to call function again. You mistyped the function name, it's getFileName (capital N), and you forgot "return". So: return getFileName(); | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply