StreamReader gets and sets position

I just want to read a large CSV file and keep the Stream position in the list. After that I need to read the position from the list and set the position of the Streamreader to char and read the line !! But after I read the first line and returned streamposition with

StreamReader r = new StreamReader("test.csv"); r.readLine(); Console.WriteLine(r.BaseStream.Position); 

I get "177" which are full characters in the file! (this is just a short sample file) I did not find anything like it here that helped me!

Why?

Full method:

 private void readfile(object filename2) { string filename = (string)filename2; StreamReader r = new StreamReader(filename); string _top = r.ReadLine(); top = new Eintrag(_top.Split(';')[0], _top.Split(';')[1], _top.Split(';')[2]); int siteindex = 0, index = 0; string line; sitepos.Add(r.BaseStream.Position); //sitepos is the a List<int> while(true) { line = r.ReadLine(); index++; if(!string.IsNullOrEmpty(line)) { if (index > seitenlaenge) { siteindex++; index = 1; sitepos.Add(r.BaseStream.Position); Console.WriteLine(line); Console.WriteLine(r.BaseStream.Position.ToString()); } } else break; maxsites = siteindex; } reading = false; } 

The file is as follows:

 name;age;city Simon;20;Stuttgart Daniel;34;Ostfildern 

And so on this is the Program: http://clean-code-advisors.com/ressourcen/application-katas (Katas CSV viewer) I am now free 3

+7
c # stream position streamreader
source share
1 answer

StreamReader uses a buffered stream, and therefore StreamReader.BaseStream.Position is likely to exceed the number of bytes that you actually "read" using ReadLine .

It discusses how to do what you are trying to do in this SO question .

+8
source share

All Articles