Solved

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

Posted on 2015-01-15
3
291 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
ID: 40552333
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
ID: 40552492
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
ID: 40553041
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 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