I'm using the following StreamReader to read from text file
string temp = fs.ReadToEnd ();
readlines[i] = temp;
I want to read a specific number of lines from the text file (let we say, from line number 1 until line number 300 only), then write the lines into one element of array. Could anyone help please? thanks in advance.
You can just use (. ignore) function that you can reiterate using a for loop n times. For example: for (int i = 0 ; i < n ; i++){
To read from a text file in C, you will need to open a file stream using the fopen() function. Once a file stream has been opened, you can then read the file line by line using the fgets() function.
The End of the File (EOF) indicates the end of input. After we enter the text, if we press ctrl+Z, the text terminates i.e. it indicates the file reached end nothing to read.
Use the ReadLine
method and add a counter
and increase it by line and when you hit 300 do a break
out of the loop
Tried with a simple text file.
var lines = File.ReadLines("yourfile").Take(300);
readlines[i] = string.Join("-", lines);
If you want to skip n first lines and read p lines from there :
var lines = System.IO.File.ReadLines(path).Skip(n).Take(p).ToArray()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With