Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Let Suppose I have following lines in an file.text. I want to read line - line

abc <tab> def <tab> ghi <tab> jkl
qwe <tab> oiu <tab> bhi <tab> iuy


How to read the line by line and put it into some variable

Help........
Posted

I think it would be useful to know whether you're interested in the file reading or the string splitting, or both?
Also what platform are you using - .NET, MFC, STL???
 
Share this answer
 
Comments
Pravin Patil, Mumbai 23-Sep-11 6:32am    
Very good observation.....
Use File.ReadAllLines to return the content of a file..
 
Share this answer
 
Comments
Pravin Patil, Mumbai 23-Sep-11 4:35am    
I think, OP wants to read the file Line-By-Line and all lines at once.
To read line by line and then assigning the read line to a variable, refer this code.

C#
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        try 
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            // So no need of any dispose functionality
            using (StreamReader sr = new StreamReader("TestFile.txt")) 
            {
                string line;

                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null) 
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (Exception e) 
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }
}


Hope this helps.
All the best.
 
Share this answer
 
Comments
Richard MacCutchan 23-Sep-11 5:11am    
I think OP wants C++ solution.
Pravin Patil, Mumbai 23-Sep-11 6:32am    
Yeah, I did not see the tag....
Use the istream::getline()[^] function to read a file line by line.
 
Share this answer
 

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