在读文件时如何区分空格与逗号等符号

alanzhu 2002-12-29 03:51:28
C++用ifstream的对象infile打开一个文件后,用infile>>word得到的一个单词是以空格为标志若是其他标点符号则无效,我该怎么办?
...全文
105 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
alanzhu 2003-01-10
  • 打赏
  • 举报
回复
对不起,
vctTagWord是什么啊
Alain_Delone 2002-12-30
  • 打赏
  • 举报
回复

建议一个字符段一个字符段读入,这里有个例子
string strLine;
const string delims(" ?\t,.;:!\'\"[{]}<%>-/)(");
int iWordCount=0;
while(getline(cntFile,strLine))
{
string::size_type begIdx,endIdx;

begIdx=strLine.find_first_not_of(delims);

while(begIdx!=string::npos)
{
endIdx=strLine.find_first_of(delims,begIdx);
if(endIdx==string::npos)
{
endIdx=strLine.length();
}

string temp("");
for(int j=begIdx;j<endIdx;++j)
{
temp.append(1,strLine.at(j));
}

cout<<endl<<temp;

++iWordCount;

if(find(vctTagWord.begin(),vctTagWord.end(),temp)==vctTagWord.end())
{
map<string,int>::iterator pos;
pos=mpWord.find(temp);

if(pos!=mpWord.end())
{
++pos->second;
}
else
{
mpWord.insert(make_pair(temp,1));

}

}

begIdx=strLine.find_first_not_of(delims,endIdx);

}
shornmao 2002-12-30
  • 打赏
  • 举报
回复
你问这个问题,是因为对英文的排版不了解的缘故,在英文排版中,要求标点符号后面必须用空白和下一个单词分隔,但是标点符号本身可以和前一个单词不分隔。

不过,rushman(一线天)的方法是正解。
rushman 2002-12-29
  • 打赏
  • 举报
回复
说明一下:
前面的例子中在输入中加上逗号、空格、回车,看看得到的结果是什么。
whoke 2002-12-29
  • 打赏
  • 举报
回复
gz
rushman 2002-12-29
  • 打赏
  • 举报
回复
试一下下面的例子。
//-------------------------------------------
char buf[100];

cin.getline(buf,sizeof(buf) - 1,',');
cout<<buf<<endl;
cin.getline(buf,sizeof(buf) - 1,',');
cout<<buf<<endl;
cin.getline(buf,sizeof(buf) - 1,' ');
cout<<buf<<endl;
cin.getline(buf,sizeof(buf) - 1,'\n');
cout<<buf<<endl;
sea_lover 2002-12-29
  • 打赏
  • 举报
回复
GetLine()
earthharp 2002-12-29
  • 打赏
  • 举报
回复
GetLine();
stroke

24,860

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧