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