Hansal Mehta
Hansal Mehta

Reputation: 195

How to read multilines from text file in c#

I have a text file which contains user records.In the text file one user record is present in three lines of text file.Now as per my requirement i have to read first three lines for one User, Process it and insert into database and next three lines for second user and so on..

Here is the code that i have used for single line reading from text files..

        if (System.IO.File.Exists(location) == true)
        {
            using (StreamReader reader = new StreamReader(location))
            {
                while ((line = reader.ReadLine()) != null)
                {     
                        line = line.Trim();
                }
        }   
        }

Please help me to read multi-lines , in this case 3 lines from text file ..

Thanks..

Upvotes: 0

Views: 2323

Answers (3)

Marco
Marco

Reputation: 23945

I have used a dummy dource file with this content:

line1_1 /*First line*/
line1_2
line1_3
line2_1 /*second line*/
line2_2
line2_3
line3_1 /*third line*/
line3_2
line3_3
line4_1 /*fourth line*/
line4_2
line4_3

string result = String.Empty;
string location = @"c:\users\asdsad\desktop\lines.txt";
if (System.IO.File.Exists(location) == true)
    {
        using (StreamReader reader = new StreamReader(location))
        {
            string line = String.Empty;
            while ((line = reader.ReadLine()) != null) /*line has the first line in it*/
            {
                for(int i = 0; i<2; i++) /*only iterate to 2 because we need only the next 2 lines*/
                    line += reader.ReadLine(); /*use StringBuilder if you like*/
                result += line; 
            }
    }   
    result.Dump(); /*LinqPad Only*/

Upvotes: 1

Saverio Terracciano
Saverio Terracciano

Reputation: 3915

You could do something like:

if (System.IO.File.Exists(location) == true)
        {
            var lines=File.ReadAllLines(location);
            int usersNumber = lines.Count() / 3;
            for(int i=0; i < usersNumber; i++){
                var firstField=lines[i*3];
                var secondField=lines[i*3 +1];
                var thirdField=lines[i*3 +2];
                DoStuffs(firstField,secondField,thirdField);
            }
            if(lines.Count() > usersNumber *3) //In case there'd be spare lines left
                 DoSomethingElseFrom(lines, index=(usersNumber*3 +1));
        }

You're reading all the lines of your file, counting how many users you have (group of 3), then for each group you're retrieving its associate info, and in the end you're processing the group of 3 fields related to the same user.

Upvotes: 1

Rob
Rob

Reputation: 27367

void Main()
{
    var location = @"D:\text.txt";
    if (System.IO.File.Exists(location) == true)
    {
        using (StreamReader reader = new StreamReader(location))
        {
            const int linesToRead = 3;
            while(!reader.EndOfStream)
            {
                string[] currReadLines = new string[linesToRead];
                for (var i = 0; i < linesToRead; i++)
                {
                    var currLine = reader.ReadLine();
                    if (currLine == null)
                        break;

                    currReadLines[i] = currLine;
                }

                //Do your work with the three lines here
                //Note; Partial records will be persisted
                //var userName = currReadLines[0] ... etc...
            }
        }
    }
}

Upvotes: 0

Related Questions