| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 513 人关注过本帖
标题:[求助]比较头疼的问题!
只看楼主 加入收藏
weimade
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-8-2
收藏
 问题点数:0 回复次数:5 
[求助]比较头疼的问题!

目标:做一个exe可以向文本中写内容。每次启动这个文本都可以写放内容,而不影响先前的内容。
问题:每次启动exe的时候,都会重新建一个相同的文件。而不是打开以前的那个文件。


#include <fstream>
#include <iostream>
#define M 100
const char *salarfile="c:\\salar.txt";
using namespace std;

class examplea
{public:
void input_new();
};

void examplea::input_new()
{
ofstream outfile;
outfile.open(salarfile);
if(!outfile)
{
std::cout<<"open file error!"<<endl;

}

std::cout<<"please input your name:";
char name[10];
std::cin>>name;
outfile<<name;
std::cout<<"please input your age:";
int age;
cin>>age;
outfile<<age;
outfile.close();
}


int main()
{examplea a;
a.input_new();
a.input_new();
a.input_new();
a.input_new();

return 0;
}

搜索更多相关主题的帖子: 头疼 
2006-08-02 19:34
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
#include <fstream>
#include <string>
#include <iostream>

const char *salarfile="c:\\salar.txt";
using namespace std;

class examplea
{
public:
void input_new();
};

void examplea::input_new()
{
ofstream outfile(salarfile, ios::out | ios::app);

if(!outfile.is_open())
{
cout<<"open file error!"<<endl;

}

cout<<"please input your name: ";
string name;
getline(cin, name);
outfile<<name<<" ";

cout<<"please input your age: ";
int age;
if(cin>>age)
outfile<<age<<endl;
else
{
cerr<<"type error\n";
outfile.close();
exit(1);
}
cin.get();
outfile.close();
}


int main()
{
examplea a;
a.input_new();
a.input_new();
a.input_new();
a.input_new();

return 0;
}

[此贴子已经被作者于2006-8-2 20:01:35编辑过]


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-08-02 20:00
wfpb
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:2188
专家分:0
注 册:2006-4-2
收藏
得分:0 
以下是引用weimade在2006-8-2 19:34:29的发言:

目标:做一个exe可以向文本中写内容。每次启动这个文本都可以写放内容,而不影响先前的内容。
问题:每次启动exe的时候,都会重新建一个相同的文件。而不是打开以前的那个文件。


#include <fstream>
#include <iostream>
#define M 100
const char *salarfile="c:\\salar.txt";
using namespace std;

class examplea
{public:
void input_new();
};

void examplea::input_new()
{
ofstream outfile;
outfile.open(salarfile,std::ios::app); //这个加上去的东西表示每次在文件后面追加内容,而不是重新写
if(!outfile)
{
std::cout<<"open file error!"<<endl;

}

std::cout<<"please input your name:";
char name[10];
std::cin>>name;
outfile<<name;
std::cout<<"please input your age:";
int age;
cin>>age;
outfile<<age;
outfile.close();
}


int main()
{examplea a;
a.input_new();
a.input_new();
a.input_new();
a.input_new();

return 0;
}


[glow=255,red,2]wfpb的部落格[/glow] 学习成为生活的重要组成部分!
2006-08-02 20:06
weimade
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-8-2
收藏
得分:0 

谢谢。。。

2006-08-02 20:21
weimade
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2006-8-2
收藏
得分:0 
新人,,,要加油啊,,
继续努力
2006-08-02 20:24
xinhang
Rank: 2
等 级:论坛游民
帖 子:40
专家分:57
注 册:2006-7-25
收藏
得分:0 
that is right! we have to come on!!!!

\" ﹏╭⌒╮¤ ﹋ ^^
2006-08-03 00:59
快速回复:[求助]比较头疼的问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038227 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved