【发布时间】:2013-09-14 14:32:35
【问题描述】:
这是从文本文件中获取输入的代码。我使用了字符明智的阅读。当我打印 B 时,它只在 'rao' 之后打印额外的行。
我的文本文件输入是:
1 singh
2 sen singh rao singh pal sen jain sen de rao
3
这里是代码
int main() {
ifstream in;
in.open( "Text.txt" );
string s;
while(getline(in,s)){
char x;
x=in.get();
if(x=='1'){
string s;
in>>s;
}
if (x=='2'){
char c;
c=in.get();
c=in.get();
do{
string A;
string B;
do{
A=A+c;
c=in.get();
} while(c!=' ');
c=in.get();
do{
B=B+c;
c=in.get();
} while(c!=' '&&c!='1'&&c!='2'&&c!='3'&&c!='4'&&c!='5'&&c!='6'&&c!='7');
cout<<B<<endl;
}
while(c!='1'&&c!='2'&&c!='3'&&c!='4'&&c!='5'&&c!='6'&&c!='7');
if(c!='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'){
in.putback(c);
}
}
}
}
【问题讨论】:
-
你在每一行之后连接
endl。当然,你最终会得到一个整理新线 -
您可以使用普通的
while而不是do while并在循环的开头打印endl -
即使我不使用 endl 输出也是 :"singhsinghsensenrao" 带换行符
-
建议,而不是比较所有
!=,尝试类似:unsigned int value; in >> value; } while ((value > 0) && (value < 8)); -
好吧,这里有更好的方法来传达 Q.:我将文本输入为 1 nikhil 2 amit nikhil arpit amit ramesh tushar 3(3 写在新行中)单击此处查看快照:s24。 postimg.org/kugjbp11h/utkarsh_debug.png
标签: c++