永发信息网

c++:文件写入、读取与打开

答案:2  悬赏:80  手机版
解决时间 2021-04-05 06:20
c++:文件写入、读取与打开
最佳答案
#include
using std::ifstream;
using std::ofstream;
void main()
{
//写入
string strPw;
ofstream fileOut;
fileOut.open( _TEXT( "d:\\password.txt" ) );
if ( fileOut )
{
cin >> strPw;//input pw
fileOut.write( strPw.c_str(), strPw.length() );
fileOut.close();
}

//读出
ifstream fileIn;
fileIn.open( _TEXT( "d:\\password.txt" ) );
if ( fileIn )
{
char cPw[MAX_PATH];
fileIn.getline( cPw, MAX_PATH );
fileIn.close();
}
}追问是不是少了什么,我用vs2008运行不过去追答#include
#include
#include
#include
#include
using std::ifstream;
using std::ofstream;
using std::string;
using std::cin;
void main()
{
//写入
string strPw;
ofstream fileOut;
fileOut.open( _TEXT( "d:\\password.txt" ) );
if ( fileOut )
{
cin >> strPw;//input pw
fileOut.write( strPw.c_str(), strPw.length() );
fileOut.close();
}

//读出
ifstream fileIn;
fileIn.open( _TEXT( "d:\\password.txt" ) );
if ( fileIn )
{
char cPw[MAX_PATH];
fileIn.getline( cPw, MAX_PATH );
fileIn.close();
}
}追问#include
#include
我是一个初学者,这两个头文件我们还没有接触过,可不可以把这两个里的换成别的方式来实现呢?追答#include
#include
#include
using std::ifstream;
using std::ofstream;
using std::string;
using std::cin;
void main()
{
//写入
string strPw;
ofstream fileOut;
fileOut.open( L"d:\\password.txt" );
if ( fileOut )
{
cin >> strPw;//input pw
fileOut.write( strPw.c_str(), strPw.length() );
fileOut.close();
}

//读出
ifstream fileIn;
fileIn.open( L"d:\\password.txt" );
if ( fileIn )
{
char cPw[260];
fileIn.getline( cPw, 260 );
fileIn.close();
}
}
全部回答
头文件
里面定义了ifstream, ofstream,从文件中读取和写入文件东西, 自己百度下吧
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
头一个字是乐后一个是人的成语乐()()人
蛇蛇蛇蛇
328,除以46用竖式怎么算?
As a green hand, he doesn’t know it takes
最近除了好声音这些,还有哪些选秀节目,想参
两个苹果手机用一个itunes id号就可以相互监
网络机顶盒HDMI转换VGA图像没问题音频怎么解
问下系统ghost后 是不是原来全部的文件都会一
刚刚晋升为宝爸宝妈的你们知道如何照顾小宝宝
表达对家人祝福的诗句,祝福语大全的家人祝福
单选题根据你的生活经验和所学化学知识,判断
老公婚前买的房子婚后想加上我的名字可以吗?
狗狗疫苗从哪里可以买到,别告诉我去宠物店啥
黑色的无袖连衣裙里面配了一件灰色的上衣要搭
聊城到即墨路程多远
推荐资讯
参加2018年济宁事业单位有户籍限制吗
Macbook pro 15寸高配,玩英雄联盟掉帧,请问
急求这个字念什么?急急
离河南比较近的山有哪些?
包公为何有龙头,虎头,狗头三口铡刀啊
Young women are more adventurous than youn
对非负实数x“四舍五入”到个位的值记为 <x>
呼兰区哈尔滨南京小笼包地址有谁知道?有点事
谁有魔鬼恋人血祭的汉化版??我用的手机。因
感人的诗歌知乎,关于幼教的诗歌
有没有考完access的
100块钱提成两个点是多少钱
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?