Kornel
Kornel

Reputation: 263

IO operation inside loop

I am writing a small console app which have to overwrite a txt file with another txt file, however the final executes 3 times, I think it is because the IO writing process is slower than the IO output process. Anyone can help me how can I execute the loop only once?

Here is the code:

while (confirm != 'x') {
    Console.WriteLine(
        "Do you want to copy the archive to test2.txt? (y)es or e(x)it");
    confirm = (char)Console.Read();
    if (confirm == 's') {
        File.Copy("C:\\FMUArquivos\\test.txt", 
                  "C:\\FMUArquivos\\test2.txt", true);
        Console.WriteLine("\nok\n");
    }
    Console.WriteLine("\ncounter: " + counter);
    counter += 1;
}

Upvotes: 0

Views: 133

Answers (3)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112602

If you hit y<enter> then this will give you the 3-character sequence "y" + <cr> + <lf> and will produce three iterations and therefore the counter will be increased by 3. Use ReadLine instead.

int counter = 0; 
while (true) {
    Console.WriteLine("Do you want to copy ...");
    string choice = Console.ReadLine();
    if (choice == "x") {
        break;
    }
    if (choice == "y") {
        // Copy the file
    } else {
        Console.WriteLine("Invalid choice!");
    }
    counter++;
}

Upvotes: 3

YoryeNathan
YoryeNathan

Reputation: 14522

Try this code:

var counter = 0;

Console.WriteLine("Do you want to copy the archive to test2.txt? (y)es or e(x)it");
var confirm = Console.ReadLine();

while (confirm != "x")
{
    File.Copy("C:\\FMUArquivos\\test.txt", "C:\\FMUArquivos\\test2.txt", true);
    Console.WriteLine("\nok\n");

    counter += 1;
    Console.WriteLine("\ncounter: " + counter);

    Console.WriteLine("Do you want to copy the archive to test2.txt? (y)es or e(x)it");
    confirm = Console.ReadLine();
}

It will ask if you want to continue, and if you press y (or anything other than x), it will copy the file and print "\nok\n" and "1". Then it will ask you again, and if you press x, it will stop.

Upvotes: 1

RJ Lohan
RJ Lohan

Reputation: 6527

Now that I've copied and run your code, I see your problem. You should replace your calls to 'Read' with 'ReadLine' and change the confirm type to a string and compare on that.

The reason is that Console.Read is returning only when you hit 'enter', so it reads 3 characters; 's' '\r', '\n' (the final 2 being newline on Windows).

See here for the API reference for Console.Read: http://msdn.microsoft.com/en-us/library/system.console.read.aspx

Try this;

string confirm = "";
int counter = 0;
while (confirm != "x")
{
    Console.WriteLine("Do you want to copy the archive to test2.txt? (y)es or e(x)it");
    confirm = Console.ReadLine();
    if (confirm == "s")
    {
        File.Copy("C:\\FMUArquivos\\test.txt",
            "C:\\FMUArquivos\\test2.txt", true);
        Console.WriteLine("\nok\n");
    }
    Console.WriteLine("\ncounter: " + counter);
    counter += 1;
}

Upvotes: 1

Related Questions