[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Console, ReadLine and EOF

Posted on 2010-01-05
11
Medium Priority
?
1,599 Views
Last Modified: 2012-06-22
What's the best way to use System.Console.In.ReadLine AND to test whether there are any buffered lines remaining to be processed?

I'm wanting to build a basic console app that reads one line at a time from stdin, and simply echos that line to stdout.  When there are no more lines to read, the ReadLine loop needs to exit.
0
Comment
Question by:peetm
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26180479

static void Main(string[] args)
        {
            var str = Console.ReadLine();
            var buffer = string.Empty;

            while (!string.IsNullOrEmpty(str))
            {
                buffer += str + "\r\n";
                str = Console.ReadLine();
            }
        }

Open in new window

0
 
LVL 3

Expert Comment

by:russellC
ID: 26180526
This link should show you what you need to know.

Basically you just need to create a StreamReader and StreamWriter.  Place the ReadLine property in a while loop and how you want to process each line within.  This will read every line in your file one line at a time.

http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=132
0
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26180557
What sort of stream are you intending reading from ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 26180595
Are you redirecting a file to STDIN or are you accepting user input?
0
 
LVL 10

Author Comment

by:peetm
ID: 26180609
zadeveloper:

Seems like one should be able to get ruid of the leading ReadLine though?

Would this work?

while(!string.IsNullOrEmpty(str = console.ReadLine)) { }

russellC:

How would I initialise a StreamReader using StdIn?  I've alread tried New StreamReader(System.Console.In) etc
0
 
LVL 10

Author Comment

by:peetm
ID: 26180629
kaufmed:

I want to read from stdin - so that I can 'pipe' with another console app.

I also need to write to stderr and stdout - - - I can do this, but rather than read *everything* from stdin, I really want to read a line at a time.  The question there is - when do you know another line isn't coming right? Well, I'm hoping that the environment has buffered stdin so that if I could test it against 'empty' or EOF then that'd be that.
0
 
LVL 3

Expert Comment

by:russellC
ID: 26180690
Sorry peetm I missed the fact that this is coming from a console app.  I have never done this but I went out and found another good article check this out it may be helpful.

http://www.codeproject.com/KB/cs/ProcessStartDemo.aspx
0
 
LVL 13

Accepted Solution

by:
zadeveloper earned 320 total points
ID: 26180704
try this:
 static void Main(string[] args)
        {
            using (var streamOut = Console.Out)
            {
                using (var streamIn = Console.In)
                {
                    var line = streamIn.ReadLine();
                    while (line != null)
                    {
                        streamOut.WriteLine(line);
                        line = streamIn.ReadLine();
                    }
                    streamIn.Close();
                }
                streamOut.Close();
            }
        }

Open in new window

0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 180 total points
ID: 26181362
Maybe I'm missing something here, but how about:

for (string buffer = Console.In.ReadLine(); buffer != null; buffer = Console.In.ReadLine())
      Console.WriteLine(buffer);

When there are no more lines to read ReadLine() returns null, so basically you check at the bottom of the loop, after ReadLine(), instead of before.

If you're really dead set on checking before hand you can read a character at a time, and use Console.In.Peek() to check if there are any more characters to read.
0
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26181406
Yea, this is pretty much the same as my example. The reason I expanded the code was to manage the dispose and closing of the Stream object. But your example would work just as well.
I would possibly just modifiy it slightly to: (If I understand the requirement correctly.

for (string buffer = Console.In.ReadLine(); buffer != null; buffer = Console.In.ReadLine())
      Console.Out.WriteLine(buffer);

Console.In.Close()
Console.In.Dispose()

Console.Out.Close()
Console.Out.Dispose()

Open in new window

0
 
LVL 10

Author Comment

by:peetm
ID: 26202355
tgerbert: - I like the for() loop, but I went with a variant of zadeveloper's while loop, i.e.,

 while((line = streamIn.ReadLine()) != null)

I also removed the using() and the explicit close() calls. I can't see what using's adding here - have I missed something?

Anyway, I'm going to assign points and close the Q now, but feel free to comment still!tgerbert: - I like the for() loop, but I went with a variant of zadeveloper's while loop, i.e.,

 while((line = streamIn.ReadLine()) != null)

I also removed the using() and the explicit close() calls. I can't see what using's adding here - have I missed something?

Anyway, I'm going to assign points and close the Q now, but feel free to comment still!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Loops Section Overview
Suggested Courses
Course of the Month20 days, 2 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question