Recursive function call
monarch_dodra via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Sep 24 05:27:33 PDT 2014
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");
}
More information about the Digitalmars-d-learn
mailing list