Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How should I implement such thing: I have a text file, which contains information about item in each line, one line - item title, other line - item price, third line - item description... Totaly there is 5 lines per items, and if I have 3 items there will be 15 lines.

How should I read the file line by line, that every 5 lines I could write them as one line in other text file?

Dummy info of text file nr. 1:
Lenovo netbook
356.00$
SN4589 IoS
Black
Fast computer
Lenovo 22 netbook
99.00$
SN1233 IoS
White
Slow computer


Second text file after program actions:
Lenovo netbook 356.00$ SN4589 IoS Black Fast computer
Lenovo 22 netbook 99.00$ SN1233 IoS White Slow computer


I am reading lines with such code:
System.Collections.Generic.IEnumerable<String> lines = File.ReadLines("c:\\file.txt")


How to get number of lines array (file lines) and write every 5 lines as one line to new file?
Posted
Updated 7-May-13 12:21pm
v2
Comments
Sergey Alexandrovich Kryukov 7-May-13 18:09pm    
Did you know that this is a site for software developers (and students). What is is, off-topic?
You should be able to read standard documentation and solve this problem by yourself. What did you try so far?
—SA
Tautvydas Deržinskas 7-May-13 18:22pm    
Sorry, I have updated my question.

No, File.ReadLines returns string[], each element of returned array representing, no to surprise, one line.

—SA
 
Share this answer
 
Here it is the solution. I even tested for you.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReadFromFile
{
    class Program
    {
        static void Main(string[] args)
        {
             // Store your and name the file in your drive, in my case is C:\tmp\WriteLines.txt
            // Read each line of the file into a string array. Each element 
            // of the array is one line of the file. 
            string[] lines = System.IO.File.ReadAllLines(@"C:\tmp\WriteLines.txt");
            List<string> result = new List<string>();

            // Display the file contents by using a foreach loop.
            System.Console.WriteLine("Contents of WriteLines.txt = ");
            string temp = "";
            //int count = 0;
            for (int i = 0; i < lines.Length; i++)
            {
                if ((i+1) % 5 == 0)
                {
                    result.Add(temp);
                    //count++;
                    temp = "";
                }
                else
                {
                    temp += lines[i];
                }
            }
            foreach (string s in result)
            {
                Console.WriteLine(s);
            }

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
    }
}
 
Share this answer
 
v3
Comments
Tautvydas Deržinskas 7-May-13 19:15pm    
Thank you Balimusi! Works fine. So to write to new file results I have to replace Console.WriteLine(s); yes?
Balimusi 7-May-13 19:25pm    
Yes, replace that Console.WriteLine() and write it to a file and that is stored in your drive.

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