Click here to Skip to main content
15,868,005 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
i am a beginner..
i need to read a text file line by line in the following way =>

read the first line>print it on console>carry out some function like myFunction(line)>print on the console what the function gives out>then read the next line>................

no complicated answers plz.
i know im a noob.. dont tell me that (-_-)
Posted
Comments
Patrice T 26-Jan-16 10:16am    
At least try to do it yourself. and publish your code when you are stuck.
Advice: Do basic research about your problem before asking.
Kevin Marois 26-Jan-16 10:17am    
He DID basic research. He posted his question here
Kevin Marois 26-Jan-16 10:18am    
See this:

http://lmgtfy.com/?q=how+to+read+text+from+a+text+file+line+by+line+in+c%23

There are a couple of ways: The StreamReader class has a ReadLine method for example.
But the simplest way is to read the lot at once:
C#
string[] lines = File.ReadAllLines(@"D:\Temp\MyFile.txt");
foreach (string line in lines)
   {
   Console.WriteLine(line);
   string result = DoSomething(line);
   Console.WriteLine(result);
   }
 
Share this answer
 
Comments
ZurdoDev 26-Jan-16 10:24am    
Exactly. +5.
Arasappan 27-Jan-16 1:53am    
mmmm
Ratul Thakur 28-Jan-16 5:32am    
this works well ..thanks
OriginalGriff 28-Jan-16 6:16am    
You're welcome!
Use File.Open[^] to get a stream

Create an instance of StreamReader[^] for that stream.

Use ReadLine[^] method to read the file line by line, using EndOfStream Property[^] to see if you've read the whole file.
 
Share this answer
 
it is bit simple using streamreader, see below snippet

C#
int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file = 
   new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
   Console.WriteLine (line);
   //call your function here
   counter++;
}

file.Close();

// Suspend the screen.
Console.ReadLine();
 
Share this answer
 
Comments
Ratul Thakur 28-Jan-16 5:31am    
ya i know this
the same is on microsoft's site.
i studied a bit about streamreader nd found the solution.
thanks yall. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900