c++文件输出的小问题 谢谢!

thomaslw 2005-05-30 04:46:18
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;
int main()
{

const int str_size=100;
int i=0;
vector<string> str(str_size);
string str_temp;
fstream iofile("data.txt");
if (!iofile)
cout<<"error";
else
{

while (iofile>>str_temp)
{
cout<<"successfuly read this line. "<<endl;
cout<<"this word is "<<str_temp<<endl<<endl;
str[i++]=str_temp;
}
}
cin>>i;
return 0;

}

==========================================
环境:Dev-C++ 4.9.8.0
比如data.txt里面的数据如下:"a hello world!" 运行结束后。
data.txt里面的数据就变成了:"aa hello world!" ,以后每执行一次就多一个a.
不知道什么问题,谢谢!动手太少,多多指教!
...全文
156 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
thomaslw 2005-06-02
  • 打赏
  • 举报
回复
try again-ing
thomaslw 2005-06-02
  • 打赏
  • 举报
回复
谢谢
lonelyforest 2005-05-31
  • 打赏
  • 举报
回复
用 Dev-C++ 4.9.9.2 试试看了, 或者最新的GCC 4.0 吧, 它更好的支持标准 C++ 了.
thomaslw 2005-05-31
  • 打赏
  • 举报
回复
先谢谢大家 but
======================
重新装过了.还是一样的.
======================
TO nerrymeeting(张伟)
我也以为是那个函数的问题,但是用GETLINE还是有一样的效果.
本来我想看一下fstream怎么重载的流对象输出符,是不是有什么规则,但是......那个库文件看着太痛苦了......既然大家都是正常的.怎么我的有问题呢.

===================================================
准确的版本是:Dev-C++ 5.0 beta 8 with Mingw-GCC 3.2

要是编译器出错了 真会出这种问题啊? 怎么我一个人错呢.
是不是BETA版的原因呢.不会这么巧吧.
谢谢各位,还有什么看法吗
zhangsong1238 2005-05-30
  • 打赏
  • 举报
回复
正常的呀。。你重装一下编译器吧,可能出了问题的吧
haixing511 2005-05-30
  • 打赏
  • 举报
回复
我在Dev-C++ 4.9.8.0下运行了一下,没有你说的问题阿
the777 2005-05-30
  • 打赏
  • 举报
回复
很正常!是你的编译器问题!
mostideal 2005-05-30
  • 打赏
  • 举报
回复
应该没问题呀。。
zhousqy 2005-05-30
  • 打赏
  • 举报
回复
no problem.
foochow 2005-05-30
  • 打赏
  • 举报
回复
你编译器的问题吧~~~你的程序我在VC6.0下正常运行-_-
nerrymeeting 2005-05-30
  • 打赏
  • 举报
回复

//在c++里运行通过
#pragma warning(disable:4786)
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;
int main()
{

const int str_size=100;
int i=0;
vector<string> str(str_size);
string str_temp;
fstream iofile("data.txt");
if (!iofile)
cout<<"error";
else
{
while (getline( iofile, str_temp ))//这里函数换一下。
{
cout<<"successfuly read this line. "<<endl;
cout<<"this word is "<<str_temp<<endl<<endl;
str[i++]=str_temp;
}
}
cin>>i;
return 0;

}
useresu 2005-05-30
  • 打赏
  • 举报
回复
没有写文件,怎么可能加呢?
useresu 2005-05-30
  • 打赏
  • 举报
回复
晕,,,
没有问题啊.

我也是Dev-C++ 4.9.8.0


可惜不能贴图

64,683

社区成员

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

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