vs05中输入输出流的bug?

maybe5 2009-02-02 03:02:06
如下代码在dev-c++ 中能够正确向屏幕输出代码内容,而在vs05中总会在末尾多输出一个'}',各位大大有啥解决方法么?

#include <iostream>
#include <fstream>

using namespace std;

int main(){
char c;
ifstream fin("program.cpp"); // open the current file where main() resides
if(!fin){ // check whether the file is opend successfully
return 1;
}

while(!fin.eof()){ // read and print to screen till we meet eof
fin.get(c);
cout << c;
}

fin.close(); // close the ifstream
system("pause");
return 0;
}
...全文
106 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶落寒山 2009-02-02
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
char c;

ifstream fin("program.cpp"); // open the current file where main() resides
if(!fin)
{ // check whether the file is opend successfully
return 1;
}
while(fin.get(c))
{ // read and print to screen till we meet eof
cout << c;
}
fin.close(); // close the ifstream
system("pause");
return 0;
}
maybe5 2009-02-02
  • 打赏
  • 举报
回复
只有在文件末尾继续读取,才会设置eof,任何编译器都是这样的。
呵呵,正解。
我刚才比较两者的输出也发现dev-c++会自动加空行。
[Quote=引用 9 楼 Vitin 的回复:]
dev-c++和vs05不一样,是因为dev-c++会自动在源代码最后增加一个空行,因此实际上它会输出两个空行。

只有在文件末尾继续读取,才会设置eof,任何编译器都是这样的。
所以原先的代码有问题,建议修改成这样:
while(fin.get(c)) // get的返回值可以用来判断是否到达文件末尾
cout < < c;
[/Quote]
Vitin 2009-02-02
  • 打赏
  • 举报
回复
dev-c++和vs05不一样,是因为dev-c++会自动在源代码最后增加一个空行,因此实际上它会输出两个空行。

只有在文件末尾继续读取,才会设置eof,任何编译器都是这样的。
所以原先的代码有问题,建议修改成这样:
while(fin.get(c)) // get的返回值可以用来判断是否到达文件末尾
cout << c;
taodm 2009-02-02
  • 打赏
  • 举报
回复
找msdn查一下fin.get的返回值及其相关说明。
maybe5 2009-02-02
  • 打赏
  • 举报
回复
谢谢,改成for循环解决了问题。但是能否解释的详细一点。我的理解是读完并输出最后一个}后,!fin.eof就应该终止循环了,怎么还会再打印一次呢
[Quote=引用 6 楼 hai040 的回复:]
while(!fin.eof()){ // read and print to screen till we meet eof
fin.get(c);
cout < < c;
}
-----
for(fin.get(c);!fin.eof();fin.get(c))
cout < < c;
可能vs里到eof不会改变c
[/Quote]
hai040 2009-02-02
  • 打赏
  • 举报
回复
while(!fin.eof()){ // read and print to screen till we meet eof
fin.get(c);
cout < < c;
}
-----
for(fin.get(c);!fin.eof();fin.get(c))
cout << c;
可能vs里到eof不会改变c
jichre 2009-02-02
  • 打赏
  • 举报
回复
我用这个文件进行了测试,文件内容如下:

int dfd()
{
int a;
return a;
}


输出内容如下:
int dfd()
{
int a;
return a;
}

并没有少一个}啊
maybe5 2009-02-02
  • 打赏
  • 举报
回复
main函数所在的文件就是program.cpp
[Quote=引用 3 楼 waizqfor 的回复:]
引用楼主 maybe5 的帖子:
如下代码在dev-c++ 中能够正确向屏幕输出代码内容,而在vs05中总会在末尾多输出一个'}',各位大大有啥解决方法么?

#include <iostream>
#include <fstream>
[/Quote]
waizqfor 2009-02-02
  • 打赏
  • 举报
回复
[Quote=引用楼主 maybe5 的帖子:]
如下代码在dev-c++ 中能够正确向屏幕输出代码内容,而在vs05中总会在末尾多输出一个'}',各位大大有啥解决方法么?

#include <iostream>
#include <fstream>

using namespace std;

int main(){
char c;
ifstream fin("program.cpp"); // open the current file where main() resides
if(!fin){ // check whether the file is opend successfully
return 1;
}

while(!fin.eof()){ // read and print t…
[/Quote]
program.cpp文件里什么样的啊
maybe5 2009-02-02
  • 打赏
  • 举报
回复
main函数所在的文件就是program.cpp
HelloDan 2009-02-02
  • 打赏
  • 举报
回复
那也要看你文件里面有什么内容啊

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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