How do you write a program that reads a text file and counts the number of words in that file? I'm so lost at this...summer school sucks....
Printable View
How do you write a program that reads a text file and counts the number of words in that file? I'm so lost at this...summer school sucks....
Read up on file I/O
http://www.cprogramming.com/tutorial/lesson10.html
file i/o thats what my friend c+noob is on
jaybo ill give you a hand AIM: deathbydesire101 MSN: [email protected]
ive done this script before and ill post in a sec
here ya go this reads the file for example i got it opening cool.txt in C:\
just put the directory were your file is please dont use this for the hell of it try to learn frum it. ask any questions you dont know
Code:#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int blank_count = 0;
int char_count = 0;
int sentence_count = 0;
char ch;
ifstream iFile("c:/cool.txt");
if (! iFile)
{
cout << "Error opening input file" << endl;
return -1;
}
while (iFile.get(ch))
{
switch (ch) {
case ' ':
blank_count++;
break;
case '\n':
case '\t':
break;
case '.':
sentence_count++;
break;
default:
char_count++;
break;
}
}
cout << "There are " << blank_count << " blanks" << endl;
cout << "There are " << char_count << " characters" << endl;
cout << "There are " << sentence_count << " sentences" << endl;
return 0;
}
i only got the sentance count to work for every period so that should give you a general idea
I'm not going to write your program for you, but this should give you an idea.
Modify the code so that cin becomes the input stream of the file.Code:#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
int i = 0;
while ( cin >> s ){
i++;
}
cout << "You entered " << i << " words.";
}
Summer School computer programming class? where is the drool smilie! :D
Agh, my school doesnt even have a regular computer programming course. Well, actually it does have one described but not enough people sign up. It must be a Canadian thing..
Oh yeah and for word counting, of course, pseudo-ish
Though I doubt the tutorial on this site is enough info on file I/O, maybe try about.com too.Code:make a char getChar;
make an int charCount;
open the file
while (you get char) { //takes a char in until the end of file
if charCount is equal to a space: add 1 to the charCount
}
print the charCount
>Summer School computer programming class? where is the drool smilie!
Yeah same here. My school doesn't even offer a programming class during the regular shcool year! The only classes that are computer-related are CISCO Networking and A+
even i have a bit of a time making that out the while loop would work much nicer.Quote:
Originally Posted by dra
He did it like that so the topic creator could learn about file I/O and convert it and optimize it himself, since it is for school it would be better that way (in most peoples eyes). Converting that code may even turn out nicer looking than yours :PQuote:
Originally Posted by C+noob
Not to be an ass or anything but although your code does a few interesting things, it doesn't achieve the topic creator's task which is to count words. althought you could count spaces, then add 1....Quote:
Originally Posted by C+noob
"One Two" <--- one space : 1 + 1 = 2 words. lol. But that's too much work in my opinion.
haha i thot he mmeant characters hahaha :|
Damn your lucky...My school only has 2 computer classes (c++/java and CISCO Networking) If they had more during summer school I dont care what they would be I'd take it. I love learning bout comps and stuff.Quote:
Originally Posted by dra
You'd have to change the if to...Quote:
Originally Posted by Dae
if charCount is equal to a space or newline or tab or EOF
WOW....a lot of people seemed bummed that their school doesn't have computer programming...iwell, 'll be willing to share my class with those who are less fortunate to have programming classes over the summer....
Oh yah tab, if charCount is equal to space or tab. Not EOF because the while statement ends when its at the EOF.Quote:
Originally Posted by JoshR
BTW, my school only has 2 computer classes, networking and communications, and computer study, both are just basic crap. No programming classes, the person who said they have c++/java class are lucky. :rolleyes:
He must have editted his post, because he said that he was just giving the topic creator an idea of how to do it, how to count spaces and such will give the topic creator an idea of how to count words. Which is what your topic was like also, to give the topic creator an idea, not give him the complete program.Quote:
Originally Posted by dra
But...You have to know when the EOF has been found so you can count the last word
Counting spaces does not work.Quote:
Originally Posted by dra
How many words are in this file:
Code:one two three
Ohhh yeah you're right you would if you were counting by spaces. In that case I'd use dra's code, count strings, thats the one I was thinking of cause I was playing with that code myself, it works well and easily.Quote:
Originally Posted by JoshR
dae...how do you write the code if it counts the strings?
I dont know how I can help without giving you the program, its pretty much exactly what dra's code is:Quote:
Originally Posted by jaybo684
Open the file, and instead of cin.. use the file. I dont know if using a string is more efficient than using a char and counting words based on spaces/tabs/n/and eof, but it works and the code is simple.Code:#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
int i = 0;
while ( cin >> s ){
i++;
}
cout << "You entered " << i << " words.";
}
it was just a suggestion....Quote:
Originally Posted by Rashakil Fol
besides, assuming the sentences are properly written, as in one space between each word, and two after a sentence, he could probably get away with it.
But even then, it's too much work. Letting the library do all the work is best. lol.
5.4 and 5.5 is what I have to do...
http://www.santarosa.edu/~ssarkar/ci.../cis10/a5.html
I would just use a char array for 5.5 :D
technically, you'd be reading it in character by character ;)
With all the "hints" given on this topic and on that actual page, theres a hints link, you should be able to figure it out. Dra's code implimented with what I said to do is 5.4 (with the addition of "asking for the filename to be opened (stored in a string)"). In 5.5 you simply do the same thing but instead of taking a string at a time from the file, take a char (as it says to do), and test if its a space, tab, end of line, or end of file, if it is then you add 1 to wordCount. In addition you could use a char array to store the output for 5.5, but thats pointless for this program as you can simple cout it 1 char at a time as it comes out.