其中要使用的txt文本!
header defines classes for file IO, including ifstream, whose constructor takes a file name an argument. The expression f >> word extracts the next non-whitespace token from the file and returns the stream. When a stream appears
in a boolean context, such as the while statement above, it evaluates to true until end-of-file reached or an error occurs.
It turns out that on many operating systems you can take advantage of i/o redirection, which allows you to map standard input or output to a file on the command line. If you rewrite Words.cpp to read from cin, then you can read any file you want, as the following
illustrates.
源程序
/** * 功能:统计txt文本中单词的个数 * 时间:2014年8月9日09:09:33 * 作者:cutter_point */ #include<iostream> #include<string> #include<stdlib.h> #include<fstream> #include<bitset> using namespace std; int main() { ifstream fin("words.txt"); string word; int wordNo=0; while(fin>>word) { ++wordNo; } std::bitset<8> b2(wordNo); cout<<"一共有:2进制"<<b2<<"个单词"<<endl; cout<<"一共有:8进制"<<oct<<wordNo<<"个单词"<<endl; cout<<"一共有:10进制"<<wordNo<<"个单词"<<endl; cout<<"一共有:16进制"<<hex<<wordNo<<"个单词"<<endl; system("pause"); return 0; }
【ThinkingInC++】4、统计txt文本中单词的个数
时间: 2024-11-17 20:21:02