10 Replies - 792 Views - Last Post: 28 March 2013 - 06:56 AM Rate Topic: -----

#1 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 280
  • Joined: 22-July 09

How to reprint a line in the console

Posted 26 March 2013 - 02:35 PM

I am having issues reprinting a line to the console. Here is my code:
        internal void ReportProgress(int CurrentIndex, string FileName)
        {
            Console.Write("\rProgress: " + ((decimal)CurrentIndex / (decimal)FullFileList.Length).ToString("p2") + '\t');
            Console.Write("Files remaining: " + (FullFileList.Length - CurrentIndex));
        }



I know about nto accessing things directly like that but its for a test. right now it prints everything to one line fine. But say the number of files remaining loses a digit (i.e. 10,000 to 1,000) then the remaining zero is still there. How can I avoid this issue?

I would like to keep this information on one line if possible.

Is This A Good Question/Topic? 0
  • +

Replies To: How to reprint a line in the console

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4311
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

Re: How to reprint a line in the console

Posted 26 March 2013 - 02:44 PM

I'm afraid I don't understand your problem. Perhaps if you could take a screenshot of the output and show us what you would actually like it to be.
Was This Post Helpful? 0
  • +
  • -

#3 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 280
  • Joined: 22-July 09

Re: How to reprint a line in the console

Posted 26 March 2013 - 03:00 PM

Problem appears fixed as so:
        internal void ReportProgress(int CurrentIndex, string FileName)
        {
            Console.Write("\rProgress: " + ((decimal)CurrentIndex / (decimal)FullFileList.Length).ToString("p2") + '\t');
            Console.Write("Files remaining: " + (FullFileList.Length - CurrentIndex) + ' ');
        }



It is hard to capture this on my system as it counts down a bit quickly.
Was This Post Helpful? 0
  • +
  • -

#4 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1175
  • View blog
  • Posts: 1,661
  • Joined: 24-August 11

Re: How to reprint a line in the console

Posted 26 March 2013 - 03:02 PM

If you want a new progress, wouldn't you just call the method again? FullFileList.Length should reflect the new length of the FullFileList, so the output should still be correct as long as you're not hard-coding values, right? To be fair though, I'm reasonably certain I'm not understanding this question...
Was This Post Helpful? 0
  • +
  • -

#5 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 280
  • Joined: 22-July 09

Re: How to reprint a line in the console

Posted 26 March 2013 - 03:10 PM

Example output:
Progress 99.00%     Files remaining: 100



But then once it decrements files remaining by 1 you then get:
Progress 99.00%     Files remaining: 990


This post has been edited by blank_program: 26 March 2013 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: How to reprint a line in the console

Posted 26 March 2013 - 03:27 PM

Please show your entire loop code
Was This Post Helpful? 0
  • +
  • -

#7 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 280
  • Joined: 22-July 09

Re: How to reprint a line in the console

Posted 26 March 2013 - 03:43 PM

View PosttlhIn`toq, on 26 March 2013 - 05:27 PM, said:

Please show your entire loop code


Here you go. My class is pretty big and I would rather nto be judged on poor code but this is the only place that calls ReportProgress. This is a full console application only no real GUI.
        internal void HashData(string HashType = "md5")
        {
            if (FullFileList != null)
            {
                for (int i = 0; i < FullFileList.Count; i++)
                {
                    if (i == 0 && HashType.Length > 0)
                    {
                        DataLogger.WriteApplicationLog("\nHashing data as " + HashType.ToUpper());
                    }
                    else if (i == 0 && HashType.Length == 0)
                    {
                        DataLogger.WriteApplicationLog("Hashing data as MD5");
                    }

                    if (File.Exists(FullFileList[i].FullName))
                    {
                        switch (HashType)
                        {
                            case "md5":
                                Hasher.HashFileMD5(FullFileList[i].FullName);
                                break;
                            case "sha1":
                                Hasher.HashFileSHA1(FullFileList[i].FullName);
                                break;
                            case "sha256":
                                Hasher.HashFileSHA256(FullFileList[i].FullName);
                                break;
                            case "sha512":
                                Hasher.HashFileSHA512(FullFileList[i].FullName);
                                break;
                            default:
                                Hasher.HashFileMD5(FullFileList[i].FullName);
                                break;
                        }

                        if (i != 0)
                        {
                            ReportProgress(i + 1, FullFileList[i].FullName);
                        }
                        else
                        {
                            Console.WriteLine();
                            ReportProgress(i, FullFileList[i].FullName);
                        }
                    }
                }

                DataLogger.WriteApplicationLog("Data hashing complete.");
            }
        }

Was This Post Helpful? 0
  • +
  • -

#8 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Re: How to reprint a line in the console

Posted 27 March 2013 - 07:48 PM

So... are you saying you want the file progress to stay on one line instead of each one writing a new line? In the case, the best thing to do is use Console.Clear().
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,578
  • Joined: 05-May 12

Re: How to reprint a line in the console

Posted 28 March 2013 - 05:04 AM

Console.Clear() will clear the entire console. This will make the UI flash like crazy. Console.SetCursorPosition() will let you move the cursor to where you need to be and overwrite what was previously written.

http://msdn.microsof...orposition.aspx
Was This Post Helpful? 0
  • +
  • -

#10 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1175
  • View blog
  • Posts: 1,661
  • Joined: 24-August 11

Re: How to reprint a line in the console

Posted 28 March 2013 - 05:28 AM

View Postblank_program, on 26 March 2013 - 05:10 PM, said:

Example output:
Progress 99.00%     Files remaining: 100



But then once it decrements files remaining by 1 you then get:
Progress 99.00%     Files remaining: 990



So what you're saying, basically, is that your subtraction isn't really working? 100 - 1 = 990 instead of 99? If that's the case, you've got a display error of some kind. Either 100 was really 1000, but you're only allowing three characters to be output, or you're somehow adding on a zero, or something else that I won't think of until my first cup of coffee has kicked in.

Try this: instead of doing your math in the method calls, create filesRemaining and percentComplete (or whatever), put some watches on them, as well as currentIndex and FullFileList (so you can watch the length). Watch them all increment/decrement, and look for anomalies. If they all behave correctly, then you have a simple console output issue. If not, you should be on track to figuring it out.

This post has been edited by h4nnib4l: 28 March 2013 - 05:28 AM

Was This Post Helpful? 0
  • +
  • -

#11 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Re: How to reprint a line in the console

Posted 28 March 2013 - 06:56 AM

View PostSkydiver, on 28 March 2013 - 05:04 AM, said:

Console.Clear() will clear the entire console. This will make the UI flash like crazy. Console.SetCursorPosition() will let you move the cursor to where you need to be and overwrite what was previously written.

http://msdn.microsof...orposition.aspx


Is there a good way to ensure you're writing on the right line, though? If a console application is called on an existing console window you can never really be sure where you're writing. If you clear the whole thing and then "redraw" it, you know you're good. And as long as you're not redrawing too fast, it won't flicker.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1