none
how to count no of lines in text file RRS feed

  • Question

  • hi all

       i am getting data from database and write in to text file.

    my text file look like this

    header

    header
    ---one line free space
    data   --i want to count from here
    data
    dat
    data
    data   ---stop here
    control| print the count here

    please help me!!

    Monday, June 18, 2012 11:31 AM

Answers

  • Hope  File.ReadAllLines helps you out.

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Monday, June 18, 2012 11:37 AM
  • Once you finish writing data to file, call the below function, passing file path,

    private void WriteLineCountToFile(string filePath)
    {
        long lineCount = File.ReadAllLines(filePath).LongLength - 3;
        using (var stream = File.AppendText(filePath))
        {
            stream.Write(string.Format("---- {0} Lines ------", lineCount.ToString()));
        }
    }

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Proposed as answer by Kris444 Monday, June 18, 2012 1:07 PM
    • Marked as answer by Mike FengModerator Wednesday, June 20, 2012 6:46 AM
    Monday, June 18, 2012 11:39 AM

All replies

  • Hope  File.ReadAllLines helps you out.

    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Monday, June 18, 2012 11:37 AM
  • Once you finish writing data to file, call the below function, passing file path,

    private void WriteLineCountToFile(string filePath)
    {
        long lineCount = File.ReadAllLines(filePath).LongLength - 3;
        using (var stream = File.AppendText(filePath))
        {
            stream.Write(string.Format("---- {0} Lines ------", lineCount.ToString()));
        }
    }

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    • Proposed as answer by Kris444 Monday, June 18, 2012 1:07 PM
    • Marked as answer by Mike FengModerator Wednesday, June 20, 2012 6:46 AM
    Monday, June 18, 2012 11:39 AM
  • i have to write line count of the same file at the end..

    Monday, June 18, 2012 11:43 AM
  • Hi,

    You can do it as done below

    int fl = File.ReadAllLines("C:\\temp1.text").Count();


    MCTS- Please mark posts as answered where appropriate.

    Monday, June 18, 2012 11:53 AM
  • hi

    my actual work is i have to write line count in the same file at the end.



    Monday, June 18, 2012 11:58 AM
  • private void WriteLineCountToFile(string filePath) { long lineCount = File.ReadAllLines(filePath).LongLength - 3;

    string[] readText = File.ReadAllLines(filePath);

    string [] writeText = new string[readText.Length];

    int writePos =0;

    foreach (string s in readText)
            {
                writeText[writePos] = readText[writePos] + " " + writePos.ToString();
            }
    File.WriteAllLines(path, writeText);


    }



    Mark Answered, if it solves your question and Vote if you found it helpful.
    Rohit Arora

    Monday, June 18, 2012 12:13 PM
  • Hello Selvakumar, Did you actually try my code? I does write line count to the same file at the end. I have posted the code only after testing it here.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Monday, June 18, 2012 2:36 PM
  • dear Adavesh i am using vs 2005 but var is from vs 2008 i think so, but i found other way to count the lines.

    thanks alot Adavesh. thanks u so much!!

    Tuesday, June 19, 2012 6:26 AM