燃烧到一个char阵列,一次一个char,没有琴弦
I'm trying to get input from a file, but I need to do it one char at a time into a character array. There are no numbers or symbols, but it only prints out the first letter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用
infile.eof()
是错误的。将读取操作转移到循环条件中。并且,请考虑使用infile.get(...)
而不是infile>> ...
在这种情况下。但是,更重要的是,您的代码仅打印出1个字符,因为您是
返回
'在第一个循环迭代结束时。摆脱返回
语句在循环中,它根本不属于那里。另外,您的
cout
在循环内部的语句正在打印错误的数组元素,因为您正在将读取字符传递给cout
。而且,如果输入文件中有35个字符,则您的循环具有潜在的缓冲区溢出。改用此循环:
Your use of
inFile.eof()
is wrong. Move the read operation into the loop condition instead. And, consider usinginFile.get(...)
instead ofinFile >> ...
in this situation.But, more importantly, your code prints out only 1 character because you are
return
'ing at the end of the 1st loop iteration. Get rid of thereturn
statement inside the loop, it doesn't belong there at all.Also, your
cout
statement inside the loop is printing out the wrong array element, because you are incrementingnumofCharacters
before passing the read character tocout
. And, your loop has a potential buffer overflow, if the input file has more than 35 characters in it.Try this loop instead: