Solved

Count Number of Lines in text file c# and also write to the same file .

Posted on 2015-01-15
3
270 Views
Last Modified: 2015-01-19
hello ,

I am trying to populate a field in my text file and it needs the number of records in the text file, meaning the lines.
each time I try to use the code below , I get an error saying that the file is being used as I am writting other records to it .
 
                 int count = 0;
                string line;
                TextReader reader1 = new StreamReader(@"\sqlfile.txt");
                while ((line = reader1.ReadLine()) != null)
                {
                  count++;
                }
                reader.Close();
0
Comment
Question by:valentinemhlanga
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
Kimputer earned 450 total points
Comment Utility
Open, write, close.
Then open, read, close. Etc.

Did you make a typo by the way?  (Reader.close should be reader1.close? )
0
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 50 total points
Comment Utility
Just to add to the above post...

In pas I worked a lot with txt files (e.g., code files, etc.) and found it useful to load file content into a list (of strings). I.e., each line becomes a list item in a memory. In this case it's easy to address any line.
After manipulating  the content I dumped the text back to the file. Later I found this was quite common approach.
0
 
LVL 35

Assisted Solution

by:Kimputer
Kimputer earned 450 total points
Comment Utility
Also to add to the above post...
If indeed a LOT of operations are done (and the read/write file is big), it's true it will speed up:

near beginning of program
open, read to memory (array/list or some sorts), close. Etc.
Operate/Write/read array/list(s)
open, write from memory, close file.
near beginning of program
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now