Solved

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

Posted on 2015-01-15
3
283 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

16 Experts available now in Live!

Get 1:1 Help Now