C++相同的程序:两种不同的结果。也许是由于运算符>>?
我需要你对出了什么问题的意见。
我在家中使用 Bloodsheed 编写了一个程序并得到了想要的结果。该程序的目的是显示源文件中的行以输出具有一定宽度的文本。源文件无法逐行分析。相反,应该使用字符和字符串字来读取它。
然后我去uni使用TextPad和Borland提交了我的程序:输出是不同的:单词之间的空格和一些行尾字符被忽略。我不明白发生了什么事。我花了一整天的时间处理这个案子,但没有成功。编译器是否以不同的方式使用运算符>>读取字符串?看起来在第一种情况下它会在第二种情况下的空格或行尾字符之前停止,并丢弃它们。您对这个问题有什么建议吗?
在家里成功的输出是:
Max line length: 40
___Inglis_(1994)_describes_it_thus:
"For_output_of___floating-point_numbers,
the_format_strings_used_by_printf_may
include_%f_or_%e_(or_%g,_which_we_will
ignore).__These_have_much_in_common_with
%i:
____"A_minus_sign_indicates_left
justification,_a_plus_sign_indicates
that_the_converted_value_will_start_with
a_plus_sign_if_it_is_positive,_and_a
minimum_field_width_and/or_a_precision
may_be_specified.
在 uni:
Max line length: 40
___Inglis(1994)describesitthus:
"Foroutputof__floating-pointnumbers,the
formatstringsusedbyprintfmayinclude%for
%e(or%g,whichwewillignore)._Thesehave
muchincommonwith%i:
____"Aminussignindicatesleft
justification,aplussignindicatesthatthe
convertedvaluewillstartwithaplussignifit
ispositive,andaminimumfieldwidthand/ora
precisionmaybespecified.
出错的函数:
void Text::display(ofstream & out)
{ ifstream from(infileName.c_str());
if (from.fail())
{ cerr<<infileName<<" not open\n";
exit(1);
}
out<<"Max line length: "<<lineLength<<endl<<endl;
string s, w; //s stands for space, w for word
char l; //l stands for letter
int c=0; //c syands for count
while(true)
{ if(static_cast<int>(w.length())>0)
{ if(lineLength<w.length())
{ cerr <<"The line length "<<lineLength
<<" is not long enough.\n"<<"The longuest word is "
<<w<<" and has "<<w.length()
<<" letters.\n";
exit(1);
}
c+=w.length();
out<<w;
w.erase();
}
from.get(l);
if (from.fail())
{ out<<endl;
break;
}
if (l=='\n')
{ out<<l;
s.erase();
c=0;
continue;
}
while (l==' ')
{ s.push_back('_');
c++;
from.get(l);
}
if (l=='\n')
{ out<<l;
s.erase();
c=0;
continue;
}
from.putback(l);
from>>w;
c+=w.length();
if (lineLength<c)
{ out<<endl;
s.erase();
c=0;
}
else if(w.length()>0)
{ out<<s<<w;
w.erase();
s.erase();
}
}
}
I need your opinion of what is going wrong.
From home I wrote a program using Bloodsheed and got the wanted result. The purpose of the programm is to display lines from a source file to output a text with a certain width. The source file can not be analysed line by line. Instead it should be read using char and string word.
Then I went to uni to submit my program using TextPad and Borland: the output is different: spaces between words and some end of line characters are ignored. I do not understand what is going on. I have spent the all day on the case unsuccessfully. Do compiler use differently the operator >> to read string? It looks like in the first case it stops before the space or end of line character in the second one it discard them. Have you got a suggestion about the problem?
At home the successful output is:
Max line length: 40
___Inglis_(1994)_describes_it_thus:
"For_output_of___floating-point_numbers,
the_format_strings_used_by_printf_may
include_%f_or_%e_(or_%g,_which_we_will
ignore).__These_have_much_in_common_with
%i:
____"A_minus_sign_indicates_left
justification,_a_plus_sign_indicates
that_the_converted_value_will_start_with
a_plus_sign_if_it_is_positive,_and_a
minimum_field_width_and/or_a_precision
may_be_specified.
At uni:
Max line length: 40
___Inglis(1994)describesitthus:
"Foroutputof__floating-pointnumbers,the
formatstringsusedbyprintfmayinclude%for
%e(or%g,whichwewillignore)._Thesehave
muchincommonwith%i:
____"Aminussignindicatesleft
justification,aplussignindicatesthatthe
convertedvaluewillstartwithaplussignifit
ispositive,andaminimumfieldwidthand/ora
precisionmaybespecified.
function which is going wrong:
void Text::display(ofstream & out)
{ ifstream from(infileName.c_str());
if (from.fail())
{ cerr<<infileName<<" not open\n";
exit(1);
}
out<<"Max line length: "<<lineLength<<endl<<endl;
string s, w; //s stands for space, w for word
char l; //l stands for letter
int c=0; //c syands for count
while(true)
{ if(static_cast<int>(w.length())>0)
{ if(lineLength<w.length())
{ cerr <<"The line length "<<lineLength
<<" is not long enough.\n"<<"The longuest word is "
<<w<<" and has "<<w.length()
<<" letters.\n";
exit(1);
}
c+=w.length();
out<<w;
w.erase();
}
from.get(l);
if (from.fail())
{ out<<endl;
break;
}
if (l=='\n')
{ out<<l;
s.erase();
c=0;
continue;
}
while (l==' ')
{ s.push_back('_');
c++;
from.get(l);
}
if (l=='\n')
{ out<<l;
s.erase();
c=0;
continue;
}
from.putback(l);
from>>w;
c+=w.length();
if (lineLength<c)
{ out<<endl;
s.erase();
c=0;
}
else if(w.length()>0)
{ out<<s<<w;
w.erase();
s.erase();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是不同换行表示的症状。
在“home”,换行符是 LF(
'\n'
或0x0A
)。在“uni”处,换行符为 CR+LF(
'\r\n'
或0x0D0A
)。您的代码仅允许 LF 换行符。
顺便说一句...
C++ 允许标识符长于单个字符。以下内容更具表现力,无需注释,并且将使维护代码变得更加容易。
This is symptomatic of different newline representations.
At "home", your newlines are LF (
'\n'
or0x0A
).At "uni", your newlines are CR+LF (
'\r\n'
or0x0D0A
).Your code only allows for LF newlines.
As an aside...
C++ allows for identifiers longer than a single character. The following is more expressive, removes the need for comments, and will make maintaining your code much easier.