473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

text file

hello,

I'm new at this and need pointers on how I can read a file (text or xml)
from the file system and count the number of words in that file. Any ideas?

thanks
Troy
Nov 16 '05 #1
5 1242
Hi Troy,

I'm sure there is a better way to count the words, but this will work

StreamReader sr = new StreamReader("MyFile");
string data = sr.ReadToEnd();
sr.Close();

string[] words = data.Split(null); // splits at all blank characters

int count = 0;
foreach(string s in words)
{
if((s.Trim()).Length > 0)
count++;
}

count will now be the number of strings of at least 1 character length.
You may want to refine the count and ignore symbols. Regex might be
suitable for this.
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
holy that was fast - thanks man...

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opr7khh7r2hntkfz@localhost...
Hi Troy,

I'm sure there is a better way to count the words, but this will work

StreamReader sr = new StreamReader("MyFile");
string data = sr.ReadToEnd();
sr.Close();

string[] words = data.Split(null); // splits at all blank characters

int count = 0;
foreach(string s in words)
{
if((s.Trim()).Length > 0)
count++;
}

count will now be the number of strings of at least 1 character length.
You may want to refine the count and ignore symbols. Regex might be
suitable for this.
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #3
lol I recomended that to someone else and got chewed a new one...something
about way too much memory usage. If it works....

On 06/05/2004 "troy" <tr**@ttech.com> wrote:
holy that was fast - thanks man...

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opr7khh7r2hntkfz@localhost...
Hi Troy,

I'm sure there is a better way to count the words, but this will work

StreamReader sr = new StreamReader("MyFile");
string data = sr.ReadToEnd();
sr.Close();

string[] words = data.Split(null); // splits at all blank characters

int count = 0;
foreach(string s in words)
{
if((s.Trim()).Length > 0)
count++;
}

count will now be the number of strings of at least 1 character length.
You may want to refine the count and ignore symbols. Regex might be
suitable for this.
Happy coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #4
Yep, I suspect it is highly inefficient, but it should work.

Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #5
Scatropolis <ch*****@frayed.net> wrote:
lol I recomended that to someone else and got chewed a new one...something
about way too much memory usage. If it works....


You certainly don't need to read the whole thing in one go. You could
loop round on a line-by-line basis using StreamReader.ReadLine().

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
1
by: Rigga | last post by:
Hi, I am new to Python and need to parse a text file and cut parts out i.e. say the text file contained 5 rows of text: line 1 of the text file line 2 of the text file line 3 of the text...
27
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there...
16
by: thenightfly | last post by:
Ok, I know all about how binary numbers translate into text characters. My question is what exactly IS a text character? Is it a bitmap?
7
by: Chris | last post by:
Hi I can use a text file as a datasource but am unable to get the datatable to see the text file as having multiple columns. Everything gets put into the first column in the datatable. Sample of...
3
by: bbepristis | last post by:
Hey all I have this code that reads from one text file writes to another unless im on a certian line then it writes the new data however it only seems to do about 40 lines then quits and I cant...
1
by: Osoccer | last post by:
...to a different folder and in the relocated file concatenates all of the lines in one long string with a space between each line element. Here is a fuller statement of the problem: I need a...
10
by: bluemountain | last post by:
Hi there, Iam new to python forms and programming too I had a text file where i need to extract few words of data from the header(which is of 3 lines) and search for the keyword TEXT1, TEXT2,...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.