Click here to Skip to main content
15,914,014 members
Home / Discussions / C#
   

C#

 
AnswerRe: How do I respond to a protocol like http://, but more like myProtocol:// ? Pin
Chadwick Posey21-Mar-07 8:11
Chadwick Posey21-Mar-07 8:11 
GeneralRe: How do I respond to a protocol like http://, but more like myProtocol:// ? Pin
Program.X21-Mar-07 12:19
Program.X21-Mar-07 12:19 
QuestionQuestion about Web Services Pin
Khoramdin21-Mar-07 7:07
Khoramdin21-Mar-07 7:07 
Questionmonitor folder Pin
arkiboys21-Mar-07 7:02
arkiboys21-Mar-07 7:02 
AnswerRe: monitor folder Pin
Laxman Auti21-Mar-07 7:19
Laxman Auti21-Mar-07 7:19 
GeneralRe: monitor folder Pin
arkiboys21-Mar-07 22:44
arkiboys21-Mar-07 22:44 
QuestionHow to get the count of lines in .txt file Pin
CodeItWell21-Mar-07 6:59
CodeItWell21-Mar-07 6:59 
AnswerRe: How to get the count of lines in .txt file Pin
Edmundisme21-Mar-07 7:32
Edmundisme21-Mar-07 7:32 
I'm not sure if you can do anything but brute force it. Here's one way:

// Read the file into a byte array
FileStream fs = File.Open("TextFile1.txt", FileMode.Open);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, (int)fs.Length);

// int to store num of lines
int numOfLines = 0;

// if the file is not empty, we have at least 1 line
if (bytes.Length > 0) numOfLines++;

// increment the count for each newline character found
foreach (byte b in bytes) if(b == '\n') numOfLines ++;

...

This is just a starting point and would need to be more robust depending on the requirements. If the textfile's length is greater than int.MaxValue I suppose you'd need to read the file in sections. This solution will count all lines (even blank lines and trailing blank lines).

I would be interested to see another solution. I'm sure there must be a better one.

Cheers,

Ian

GeneralRe: How to get the count of lines in .txt file Pin
Wayne Phipps21-Mar-07 9:24
Wayne Phipps21-Mar-07 9:24 
GeneralRe: How to get the count of lines in .txt file Pin
Edmundisme21-Mar-07 9:44
Edmundisme21-Mar-07 9:44 
AnswerRe: How to get the count of lines in .txt file Pin
Dave Kreskowiak21-Mar-07 8:47
mveDave Kreskowiak21-Mar-07 8:47 
AnswerRe: How to get the count of lines in .txt file Pin
sherifffruitfly21-Mar-07 18:17
sherifffruitfly21-Mar-07 18:17 
Questionrecieving output from dos Pin
sharpiesharpie21-Mar-07 6:27
sharpiesharpie21-Mar-07 6:27 
AnswerRe: recieving output from dos Pin
szukuro21-Mar-07 6:36
szukuro21-Mar-07 6:36 
AnswerRe: recieving output from dos Pin
Dan Neely21-Mar-07 6:51
Dan Neely21-Mar-07 6:51 
AnswerRe: recieving output from dos Pin
Stefan Troschuetz21-Mar-07 6:54
Stefan Troschuetz21-Mar-07 6:54 
QuestionStrange behaviour Pin
sinosoidal21-Mar-07 6:17
sinosoidal21-Mar-07 6:17 
AnswerRe: Strange behaviour Pin
Stefan Troschuetz21-Mar-07 6:49
Stefan Troschuetz21-Mar-07 6:49 
GeneralRe: Strange behaviour Pin
sinosoidal21-Mar-07 6:50
sinosoidal21-Mar-07 6:50 
GeneralRe: Strange behaviour Pin
sinosoidal21-Mar-07 6:53
sinosoidal21-Mar-07 6:53 
GeneralRe: Strange behaviour Pin
Stefan Troschuetz21-Mar-07 6:58
Stefan Troschuetz21-Mar-07 6:58 
GeneralRe: Strange behaviour Pin
sinosoidal21-Mar-07 7:39
sinosoidal21-Mar-07 7:39 
AnswerRe: Strange behaviour Pin
Edmundisme21-Mar-07 7:05
Edmundisme21-Mar-07 7:05 
GeneralRe: Strange behaviour Pin
sinosoidal21-Mar-07 7:33
sinosoidal21-Mar-07 7:33 
GeneralRe: Strange behaviour Pin
Edmundisme21-Mar-07 7:42
Edmundisme21-Mar-07 7:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.