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 to extract text from text file and store it in a variable using c#

how to extract text from text file and store it in a variable using c#

Scheduled Pinned Locked Moved C#
csharptutorial
12 Posts 6 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.
  • S Offline
    S Offline
    Syed_Owais
    wrote on last edited by
    #1

    Hey Kindly do me a favor and tell me

    OriginalGriffO D S L 4 Replies Last reply
    0
    • S Syed_Owais

      Hey Kindly do me a favor and tell me

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Syed_Owais wrote:

      Hey Kindly do me a favor and tell me

      Tell you what? We have no idea what you need to know - or even what you already do know. Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. So we have no idea what is "in" your file, how it is stored, what kind of file it is in - and there are a massive number of possibilities - or even if you know how to read anything from a file! Calling it "a text file" tells us very little: there are large number of ways that text can be used to store into, and an equally large number of different types of information you coudl store in it! So sit down, think about what you are trying to do, look at your data file, and try to clarify in your own mind exactly what help you need, then try to explain that.

      Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      D 2 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        Syed_Owais wrote:

        Hey Kindly do me a favor and tell me

        Tell you what? We have no idea what you need to know - or even what you already do know. Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. So we have no idea what is "in" your file, how it is stored, what kind of file it is in - and there are a massive number of possibilities - or even if you know how to read anything from a file! Calling it "a text file" tells us very little: there are large number of ways that text can be used to store into, and an equally large number of different types of information you coudl store in it! So sit down, think about what you are trying to do, look at your data file, and try to clarify in your own mind exactly what help you need, then try to explain that.

        Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        Reading text from a text file is basically "programming 101". Simply Google for "C# read a text file" and start reading. This is far more than documented all over the web.

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Syed_Owais wrote:

          Hey Kindly do me a favor and tell me

          Tell you what? We have no idea what you need to know - or even what you already do know. Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. So we have no idea what is "in" your file, how it is stored, what kind of file it is in - and there are a massive number of possibilities - or even if you know how to read anything from a file! Calling it "a text file" tells us very little: there are large number of ways that text can be used to store into, and an equally large number of different types of information you coudl store in it! So sit down, think about what you are trying to do, look at your data file, and try to clarify in your own mind exactly what help you need, then try to explain that.

          Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          This is weird. I replied to the OP, not you. Hmmm

          Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
          Dave Kreskowiak

          OriginalGriffO 1 Reply Last reply
          0
          • S Syed_Owais

            Hey Kindly do me a favor and tell me

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Reading text from a text file is basically "programming 101". Simply Google for "C# read a text file" and start reading. This is far more than documented all over the web.

            Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
            Dave Kreskowiak

            B 1 Reply Last reply
            0
            • D Dave Kreskowiak

              This is weird. I replied to the OP, not you. Hmmm

              Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
              Dave Kreskowiak

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              It looks fine - but I got an email for it, so ... Hamsters at the Eggnog, I suspect.

              Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • S Syed_Owais

                Hey Kindly do me a favor and tell me

                S Offline
                S Offline
                SawmillTurtle
                wrote on last edited by
                #7

                I'm going to run with the assumption that you didn't mean to sound as snarky as you did. For future reference, saying "Do me a favor and tell me" is considered bad manners. It would be a good idea to explain what it is you're trying to do and then use "please" and "thank you". I'm going to give you basic instructions for reading from a raw text file. If you're trying to read binary data, this won't help much. 1. You need to start by placing "using System.IO" at the top of your project. 2. You need to declare an instance of StreamReader, open the file and read a line of text using the StreamReader. This is a watered down version of the class I created for my own projects:

                using System;
                using System.IO;

                namespace FileOperations
                {
                public class LoadData
                {
                StreamReader reader;

                	public void open(string s)
                	{
                		reader=new StreamReader(s);
                	}
                
                	public string read()
                	{
                		return reader.ReadLine();
                	}
                
                	public void close()
                	{
                		reader.Close();
                	}
                }
                

                }

                The official Microsoft documentation contains excellent instructions and another example. You can find that HERE. I break everything up like I do so that I'm not stuck reading every line in the file at once like in the Microsoft example. I can call the read() function and read one line at a time, or I can use a for or while loop to call it again and again. I can also leave the file open if I need to or close it immediately, depending on my coding needs. I would recommend that if you use this code, you implement a way to check for the end of the file. That would be as simple (as the Microsoft example shows you) as checking to see if the StreamReader returns a null string. Also, since your question shows a bit of inexperience, I feel the need to point out that you should declare a new instance of "LoadData". Here's a simple example:

                LoadData ld = new LoadData();
                ld.open("myfile.txt");
                string readLine=ld.read();
                ld.close();

                You can also simply copy the functions as they are into your existing code and call them without the need of a separate class, but by doing it this way you make this code available across your entire application. And be sure to add your own namespace at the top. Good luck, and happy coding. -Turtle EDIT: It slipped my mind to mention that you don't need to do it through function calls at all if you aren't plannin

                B L 2 Replies Last reply
                0
                • D Dave Kreskowiak

                  Reading text from a text file is basically "programming 101". Simply Google for "C# read a text file" and start reading. This is far more than documented all over the web.

                  Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                  Dave Kreskowiak

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #8

                  Another "get a brain" response that is irrelevant to the question asked. Down-voted.

                  «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

                  D 1 Reply Last reply
                  0
                  • S SawmillTurtle

                    I'm going to run with the assumption that you didn't mean to sound as snarky as you did. For future reference, saying "Do me a favor and tell me" is considered bad manners. It would be a good idea to explain what it is you're trying to do and then use "please" and "thank you". I'm going to give you basic instructions for reading from a raw text file. If you're trying to read binary data, this won't help much. 1. You need to start by placing "using System.IO" at the top of your project. 2. You need to declare an instance of StreamReader, open the file and read a line of text using the StreamReader. This is a watered down version of the class I created for my own projects:

                    using System;
                    using System.IO;

                    namespace FileOperations
                    {
                    public class LoadData
                    {
                    StreamReader reader;

                    	public void open(string s)
                    	{
                    		reader=new StreamReader(s);
                    	}
                    
                    	public string read()
                    	{
                    		return reader.ReadLine();
                    	}
                    
                    	public void close()
                    	{
                    		reader.Close();
                    	}
                    }
                    

                    }

                    The official Microsoft documentation contains excellent instructions and another example. You can find that HERE. I break everything up like I do so that I'm not stuck reading every line in the file at once like in the Microsoft example. I can call the read() function and read one line at a time, or I can use a for or while loop to call it again and again. I can also leave the file open if I need to or close it immediately, depending on my coding needs. I would recommend that if you use this code, you implement a way to check for the end of the file. That would be as simple (as the Microsoft example shows you) as checking to see if the StreamReader returns a null string. Also, since your question shows a bit of inexperience, I feel the need to point out that you should declare a new instance of "LoadData". Here's a simple example:

                    LoadData ld = new LoadData();
                    ld.open("myfile.txt");
                    string readLine=ld.read();
                    ld.close();

                    You can also simply copy the functions as they are into your existing code and call them without the need of a separate class, but by doing it this way you make this code available across your entire application. And be sure to add your own namespace at the top. Good luck, and happy coding. -Turtle EDIT: It slipped my mind to mention that you don't need to do it through function calls at all if you aren't plannin

                    B Offline
                    B Offline
                    BillWoodruff
                    wrote on last edited by
                    #9

                    Thank you for this post, which actually responds to the question asked ! Up-voted.

                    «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

                    1 Reply Last reply
                    0
                    • B BillWoodruff

                      Another "get a brain" response that is irrelevant to the question asked. Down-voted.

                      «Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      I'm sorry. Are you not enjoying your urinated Cheerio's this morning?

                      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                      Dave Kreskowiak

                      1 Reply Last reply
                      0
                      • S SawmillTurtle

                        I'm going to run with the assumption that you didn't mean to sound as snarky as you did. For future reference, saying "Do me a favor and tell me" is considered bad manners. It would be a good idea to explain what it is you're trying to do and then use "please" and "thank you". I'm going to give you basic instructions for reading from a raw text file. If you're trying to read binary data, this won't help much. 1. You need to start by placing "using System.IO" at the top of your project. 2. You need to declare an instance of StreamReader, open the file and read a line of text using the StreamReader. This is a watered down version of the class I created for my own projects:

                        using System;
                        using System.IO;

                        namespace FileOperations
                        {
                        public class LoadData
                        {
                        StreamReader reader;

                        	public void open(string s)
                        	{
                        		reader=new StreamReader(s);
                        	}
                        
                        	public string read()
                        	{
                        		return reader.ReadLine();
                        	}
                        
                        	public void close()
                        	{
                        		reader.Close();
                        	}
                        }
                        

                        }

                        The official Microsoft documentation contains excellent instructions and another example. You can find that HERE. I break everything up like I do so that I'm not stuck reading every line in the file at once like in the Microsoft example. I can call the read() function and read one line at a time, or I can use a for or while loop to call it again and again. I can also leave the file open if I need to or close it immediately, depending on my coding needs. I would recommend that if you use this code, you implement a way to check for the end of the file. That would be as simple (as the Microsoft example shows you) as checking to see if the StreamReader returns a null string. Also, since your question shows a bit of inexperience, I feel the need to point out that you should declare a new instance of "LoadData". Here's a simple example:

                        LoadData ld = new LoadData();
                        ld.open("myfile.txt");
                        string readLine=ld.read();
                        ld.close();

                        You can also simply copy the functions as they are into your existing code and call them without the need of a separate class, but by doing it this way you make this code available across your entire application. And be sure to add your own namespace at the top. Good luck, and happy coding. -Turtle EDIT: It slipped my mind to mention that you don't need to do it through function calls at all if you aren't plannin

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        Why create single line methods that simply call the real methods? That is just redundant coding. Reading from a filestream is much simpler, and usually doing it inside a using block is the optimum way.

                        1 Reply Last reply
                        0
                        • S Syed_Owais

                          Hey Kindly do me a favor and tell me

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12
                               File.WriteAllText( "data.txt", "Hallelujah, I'm a bum!" );
                               string someVariable = File.ReadAllText( "data.txt" ).Split( new char\[\] { ',', ' ' } ).First();
                               Console.Write( someVariable );
                               Console.ReadKey();
                          

                          "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                          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