Solved

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

Posted on 2015-01-15
3
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 30

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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