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....
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
I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)
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."; }
Last edited by dra; 07-10-2005 at 02:10 AM.
Summer School computer programming class? where is the drool smilie!
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
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
>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.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 :POriginally Posted by C+noob
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
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....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.Originally Posted by dra
Last edited by JoshR; 07-10-2005 at 03:27 PM.
You'd have to change the if to...Originally Posted by Dae
if charCount is equal to a space or newline or tab or EOF