|
What you want to do, Read text file Line by line or at once, if you want to read it at once then you can refer a above solution given by OriginalGriff
if you want to read it line by line use below code
using System;
using System.IO;
class Program
{
static void Main()
{
foreach (string line in File.ReadLines("c:\\file.txt"))
{
Console.WriteLine("-- {0}", line);
}
}
}
Find More .Net development tips at : .NET Tips
The only reason people get lost in thought is because it's unfamiliar territory.
|
|
|
|
|
Um...that doesn't read it line by line - it reads them all and processes them line by line in a loop afterwards.
If you want to read line by line you need to loop on StreamReader.ReadLine instead.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Yes, basically it reads it at once but later split it by lines
Find More .Net development tips at : .NET Tips
The only reason people get lost in thought is because it's unfamiliar territory.
|
|
|
|
|
Then ,how can i do it use stream ? line by line? I found Peek() ,but only can read 1 char, i need try read 1 line.
|
|
|
|
|
use below code
const Int32 BufferSize = 128;
using (var fileStream = File.OpenRead(fileName))
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize)) {
String line;
while ((line = streamReader.ReadLine()) != null)
}
Find More .Net development tips at : .NET Tips
The only reason people get lost in thought is because it's unfamiliar territory.
|
|
|
|
|
A stream doesn't know about lines; a line-feed is a character. So, you reed 1 char, until you find a new-line character, then process.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
But you explicitly say "if you want to read it line by line use below code" - which it doesn't do!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
I think you're confusing File.ReadLines[^] and File.ReadAllLines[^].
ReadAllLines reads all lines into memory and returns them as an array.
ReadLines returns an iterator that yields each line as it's read.
File.ReadLines is essentially the same as:
| | | |