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. Reading From a text file

Reading From a text file

Scheduled Pinned Locked Moved C#
questiontutorialworkspace
4 Posts 3 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.
  • G Offline
    G Offline
    Gavin_Mannion
    wrote on last edited by
    #1

    Hi All, I have a text file which will hold some details for my application. I am now trying to read these details to display them in my application. I know how to open a file and setup a StreamReader:-D, but how can I make my reader only read from a certain position or even better search for a certain keyword and then read from there? Thanks, Gavin

    J S 2 Replies Last reply
    0
    • G Gavin_Mannion

      Hi All, I have a text file which will hold some details for my application. I am now trying to read these details to display them in my application. I know how to open a file and setup a StreamReader:-D, but how can I make my reader only read from a certain position or even better search for a certain keyword and then read from there? Thanks, Gavin

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      If its a text file you should probably use a TextReader instead of a StreamReader. You can use the Seek method of the reader to move or position; but if you are looking for a specific thing you will probably have to read through the file until you find what you are looking for. James Simplicity Rules!

      G 1 Reply Last reply
      0
      • G Gavin_Mannion

        Hi All, I have a text file which will hold some details for my application. I am now trying to read these details to display them in my application. I know how to open a file and setup a StreamReader:-D, but how can I make my reader only read from a certain position or even better search for a certain keyword and then read from there? Thanks, Gavin

        S Offline
        S Offline
        SimonS
        wrote on last edited by
        #3

        Here's some code:

        static void Main(string\[\] args)
            {
        
        const int iReadTotal=10;
        const int iReadStart=10;
        
        FileStream f = new FileStream ("C:\\\\test.vbs",System.IO.FileMode.Open, System.IO.FileAccess.Read);
        f.Seek(iReadStart,System.IO.SeekOrigin.Current);
        StreamReader st = new StreamReader(f);
        
        char\[\] buffer=new char\[iReadTotal\];
        st.ReadBlock(buffer,0,buffer.Length);
        		
        string s="";
        for(int i=0;i<buffer.Length;i++)
          s+=buffer\[i\].ToString();
        
        Console.WriteLine(s);
        Console.Read();
            }
        

        HTH Cheers, Simon "I get paid for my brain and my thinking in several obscure worlds", Olli, The Lounge

        1 Reply Last reply
        0
        • J James T Johnson

          If its a text file you should probably use a TextReader instead of a StreamReader. You can use the Seek method of the reader to move or position; but if you are looking for a specific thing you will probably have to read through the file until you find what you are looking for. James Simplicity Rules!

          G Offline
          G Offline
          Gavin_Mannion
          wrote on last edited by
          #4

          I tried using the TextReader but it didn't have the seek option and I couldn't find anything else which was helpful. I need to pick up 3 different things from this file, but I can't figure out how to read through the file and select a certain thing? Or is there a better way to do this other than using a static file. I just need to store some small strings. Thanks

          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