0

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..

Hansal Mehta
  • 185
  • 2
  • 4
  • 14

3 Answers3

1

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.

Saverio Terracciano
  • 3,885
  • 1
  • 30
  • 42
  • 1
    Can you provide the reason for the negative voting? It would be more constructive since I could try to improve the answer. – Saverio Terracciano Oct 27 '14 at 10:55
  • I didn't downvote this, but first thought it this will kinda work (in principal) but will need some validation incase the number of lines comes up short. – Adrian Sanguineti Oct 27 '14 at 10:56
  • How exactly can the lines come up short? The number of lines is checked before entering the loop. – Saverio Terracciano Oct 27 '14 at 10:58
  • Well lines come up short is not quite what I meant to say. But to clarify I mean if there are 7 lines in the file, and you divide it by 3, that will give you 2 users. But there's an extra line in there which is left over which is why I am saying needs some more validation. You code should run though. – Adrian Sanguineti Oct 27 '14 at 11:05
  • Well if the file contains Users which are grouped by 3, there shouldn't be an odd line there. If there was, it would be either an error (and hence not be considered), or treated according to some business rule that the OP didn't state. – Saverio Terracciano Oct 27 '14 at 11:07
  • 1
    Which is why I said your code does work. The only thing I can fault with it is the case I gave. Only trying to guess why someone downvoted this. – Adrian Sanguineti Oct 27 '14 at 11:09
  • Added also a control in case such a scenario is interesting for the OP. – Saverio Terracciano Oct 27 '14 at 11:12
1

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*/
Marco
  • 22,856
  • 9
  • 75
  • 124
0
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...
            }
        }
    }
}
Rob
  • 26,989
  • 16
  • 82
  • 98