c++读取txt文档问题

来源:百度知道 编辑:UC知道 时间:2024/06/09 06:12:28
想读取一篇英文文章中的单词,输出到屏幕上,文章中有空格,标点,什么办法才能分辨出是不是一个单词?txt读取字符的时候,空格是不是自动跳过?

谢谢各位了!!!!!!
#pragma warning(disable:4786)
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main ()
{

int i = 0, t = 0;
vector<string> article;
char word [ 20 ];
fstream file1;
file1.open ( "e:\\wo.txt", ios::in );
if( !file1 )
{
cout << "不能打开" << endl;
return 1;
}
char ch;
file1 >> ch;
while( !file1.eof () )
{
if( (ch >= 'a') && (ch <= 'z' ) )
{
ch = ch -'a' + 'A';
}

if( (ch < 'A') && (ch > 'Z') && (ch< 'a') && (ch > 'z') )
{
t++;

if(t>=2)
{

按我的思想:
char c;
file >> c;
if ( !isalpha(c) ) // 如果不是字母
vector.push_back(word);

// 还有,你不必用字符数组,直接用string就可以了。string.push_back.

只需要这么点代码就可以,您的代码太啰嗦啦!

#include<iterator>
#include<fstream>
#include<string>
#include<vector>
#include<cctype>
using namespace std;
int main()
{
string word; vector<string> v; ifstream ifs("in.txt");
for (int c; ifs; )
if (isalpha(c = ifs.get())) word += c;
else if (!word.empty()){ v.push_back(word); word = ""; }
copy(v.begin(), v.end(), ostream_iterator<string>(cout, "\n"));
}