Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How do I write a "Hello World" string stored in a (*.txt) file to the Console output?

How do I write a "Hello World" string stored in a (*.txt) file to the Console output?

Scheduled Pinned Locked Moved C#
questionhelp
14 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • . ...---...

    How do I write a "Hello World" string stored in a (*.txt) file to the Console output? using System; using System.IO; //Is this the right idea?? //Please-- show me CODE not the "just use use a "-----" answer.... namespace StreamReaderWriter { /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { // TODO: Add code to start application here //@"C:\Documents and Settings\Desktop\Text01"; string file_name = (@"C:\Documents and Settings\Desktop\Text01"); StreamReader fReader = File.OpenText(file_name); StreamWriter fWriter = File.CreateText(@"C:\Documents and Settings\Desktop\Text01"); //Now, how do I get the "Hello World" in the text file to the console output?? } } } thanks a lot for your help.....

    C Offline
    C Offline
    Colin Angus Mackay
    wrote on last edited by
    #3

    ...---... wrote:

    Please-- show me CODE not the "just use use a "-----" answer....

    Have you tried reading the documentation for StreamReader? From MSN Search[^]you can find the top two links will take you to the StreamReader Class documentation[^]. There you will find a table with lots of handy "How to..." links. One of which takes you to Reading Text From A File[^] which shows you step-by-step exactly what you are trying to do. Does this help?


    Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

    1 Reply Last reply
    0
    • . ...---...

      How do I write a "Hello World" string stored in a (*.txt) file to the Console output? using System; using System.IO; //Is this the right idea?? //Please-- show me CODE not the "just use use a "-----" answer.... namespace StreamReaderWriter { /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { // TODO: Add code to start application here //@"C:\Documents and Settings\Desktop\Text01"; string file_name = (@"C:\Documents and Settings\Desktop\Text01"); StreamReader fReader = File.OpenText(file_name); StreamWriter fWriter = File.CreateText(@"C:\Documents and Settings\Desktop\Text01"); //Now, how do I get the "Hello World" in the text file to the console output?? } } } thanks a lot for your help.....

      N Offline
      N Offline
      numbers1thru9
      wrote on last edited by
      #4

      Sorry for my mistake, here is the correct line Console.WriteLine(fReader.ReadLine());

      1 Reply Last reply
      0
      • . ...---...

        How do I write a "Hello World" string stored in a (*.txt) file to the Console output? using System; using System.IO; //Is this the right idea?? //Please-- show me CODE not the "just use use a "-----" answer.... namespace StreamReaderWriter { /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { // TODO: Add code to start application here //@"C:\Documents and Settings\Desktop\Text01"; string file_name = (@"C:\Documents and Settings\Desktop\Text01"); StreamReader fReader = File.OpenText(file_name); StreamWriter fWriter = File.CreateText(@"C:\Documents and Settings\Desktop\Text01"); //Now, how do I get the "Hello World" in the text file to the console output?? } } } thanks a lot for your help.....

        J Offline
        J Offline
        Josh Smith
        wrote on last edited by
        #5

        ...---... wrote:

        show me CODE not the "just use use a "-----" answer....

        Are you really that lazy?

        :josh: My WPF Blog[^]

        D 1 Reply Last reply
        0
        • J Josh Smith

          ...---... wrote:

          show me CODE not the "just use use a "-----" answer....

          Are you really that lazy?

          :josh: My WPF Blog[^]

          D Offline
          D Offline
          Dan Neely
          wrote on last edited by
          #6

          Josh Smith wrote:

          Are you really that lazy?

          College is starting up again for the fall. Expect to see a majot upswing in 'do my homework for me' posts. The original poster is directed to here: http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2[^]

          1 Reply Last reply
          0
          • . ...---...

            How do I write a "Hello World" string stored in a (*.txt) file to the Console output? using System; using System.IO; //Is this the right idea?? //Please-- show me CODE not the "just use use a "-----" answer.... namespace StreamReaderWriter { /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { // TODO: Add code to start application here //@"C:\Documents and Settings\Desktop\Text01"; string file_name = (@"C:\Documents and Settings\Desktop\Text01"); StreamReader fReader = File.OpenText(file_name); StreamWriter fWriter = File.CreateText(@"C:\Documents and Settings\Desktop\Text01"); //Now, how do I get the "Hello World" in the text file to the console output?? } } } thanks a lot for your help.....

            E Offline
            E Offline
            Ennis Ray Lynch Jr
            wrote on last edited by
            #7

            Process.Start("type filename"); :p

            On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

            E C 2 Replies Last reply
            0
            • E Ennis Ray Lynch Jr

              Process.Start("type filename"); :p

              On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

              E Offline
              E Offline
              ezazazel
              wrote on last edited by
              #8

              this answer is really fantastic... :-)

              1 Reply Last reply
              0
              • . ...---...

                How do I write a "Hello World" string stored in a (*.txt) file to the Console output? using System; using System.IO; //Is this the right idea?? //Please-- show me CODE not the "just use use a "-----" answer.... namespace StreamReaderWriter { /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { // TODO: Add code to start application here //@"C:\Documents and Settings\Desktop\Text01"; string file_name = (@"C:\Documents and Settings\Desktop\Text01"); StreamReader fReader = File.OpenText(file_name); StreamWriter fWriter = File.CreateText(@"C:\Documents and Settings\Desktop\Text01"); //Now, how do I get the "Hello World" in the text file to the console output?? } } } thanks a lot for your help.....

                E Offline
                E Offline
                ezazazel
                wrote on last edited by
                #9

                for heavens sake, when a hint is given to you you should at least try it. Anyway, I also was happy when people helped me with my probs, so here is my suggestion: private void InitializeStreamReader() { System.IO.FileStream fstream = System.IO.File.OpenRead(@"c:\test.dat"); System.IO.StreamReader sreader = new System.IO.StreamReader(fstream); string[] filecontent = sreader.ReadToEnd().Split('\n'); sreader.Close(); foreach (string contentTemp in filecontent) { Console.WriteLine(contentTemp); } } Here is a short explanation what is done in this example: A Filestream is created A Streamreader which uses the Filestream the streamreader reads all the content of the file an splits it by the new lines (every new line is a new element) these elements are used in a string array the streamreader is being closed and now to the interesting part: every line in the string array is being displayed via console.writeline Of course it can be done easier, but I think you´ll like it this way as well

                . 1 Reply Last reply
                0
                • E Ennis Ray Lynch Jr

                  Process.Start("type filename"); :p

                  On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #10

                  Ennis Ray Lynch, Jr. wrote:

                  On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

                  Stealing my old signatures? :-D Would you steal my grave as quick? ;P


                  Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                  E 1 Reply Last reply
                  0
                  • E ezazazel

                    for heavens sake, when a hint is given to you you should at least try it. Anyway, I also was happy when people helped me with my probs, so here is my suggestion: private void InitializeStreamReader() { System.IO.FileStream fstream = System.IO.File.OpenRead(@"c:\test.dat"); System.IO.StreamReader sreader = new System.IO.StreamReader(fstream); string[] filecontent = sreader.ReadToEnd().Split('\n'); sreader.Close(); foreach (string contentTemp in filecontent) { Console.WriteLine(contentTemp); } } Here is a short explanation what is done in this example: A Filestream is created A Streamreader which uses the Filestream the streamreader reads all the content of the file an splits it by the new lines (every new line is a new element) these elements are used in a string array the streamreader is being closed and now to the interesting part: every line in the string array is being displayed via console.writeline Of course it can be done easier, but I think you´ll like it this way as well

                    . Offline
                    . Offline
                    ...---...
                    wrote on last edited by
                    #11

                    Ezazel Thanks a lot. I think I see the "why" ...

                    1 Reply Last reply
                    0
                    • C Colin Angus Mackay

                      Ennis Ray Lynch, Jr. wrote:

                      On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

                      Stealing my old signatures? :-D Would you steal my grave as quick? ;P


                      Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                      E Offline
                      E Offline
                      Ennis Ray Lynch Jr
                      wrote on last edited by
                      #12

                      With apologies, I just rotate through cool stuff I have copied and pasted in a sigs text file. Perhaps you can take it as a compliment rather than an offense :)

                      On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

                      C 1 Reply Last reply
                      0
                      • E Ennis Ray Lynch Jr

                        With apologies, I just rotate through cool stuff I have copied and pasted in a sigs text file. Perhaps you can take it as a compliment rather than an offense :)

                        On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

                        C Offline
                        C Offline
                        Colin Angus Mackay
                        wrote on last edited by
                        #13

                        Ennis Ray Lynch, Jr. wrote:

                        With apologies, .... Perhaps you can take it as a compliment

                        Oh no. I do take it as a compliment. I guess even the smilies around my very poor joke didn't work. (Since starting to work in Glasgow I'm picking up their very cynical sense of humour - And I move there next month so I guess it is going to get worse!)


                        Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                        E 1 Reply Last reply
                        0
                        • C Colin Angus Mackay

                          Ennis Ray Lynch, Jr. wrote:

                          With apologies, .... Perhaps you can take it as a compliment

                          Oh no. I do take it as a compliment. I guess even the smilies around my very poor joke didn't work. (Since starting to work in Glasgow I'm picking up their very cynical sense of humour - And I move there next month so I guess it is going to get worse!)


                          Upcoming Scottish Developers events: * UK Security Evangelists On Tour (2nd November, Edinburgh) * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

                          E Offline
                          E Offline
                          Ennis Ray Lynch Jr
                          wrote on last edited by
                          #14

                          Those damn irish, j/k

                          On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups