Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8035339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:16:55+00:00 2026-06-05T02:16:55+00:00

I am writing a small console app which have to overwrite a txt file

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:16:56+00:00Added an answer on June 5, 2026 at 2:16 am

    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++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a small App in which i read some text from to console,
I have a small C# console app I am writing. I would like the
I'm writing small utility app (JBoss Netty based) which should perform some trivial login
I'm writing small VB.Net app which should build reports based on data gathered from
I have a java console app that I'm writing, and I want people to
I am writing a small app ( initApp.js , initApp.routinj.js , initApp.controller.js ) which
I'm trying to practice my F# by writing small console scripts in F# in
I am writing a small program in Visual Studio 11 (beta) that a console
I have an application where I am reading and writing small blocks of data
I am writing a small console application in C# that references a custom assembly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.