永发信息网

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

答案:2  悬赏:80  手机版
解决时间 2021-04-05 06:20
  • 提问者网友:谁的错
  • 2021-04-04 16:12
c++:文件写入、读取与打开
最佳答案
  • 五星知识达人网友:拜訪者
  • 2021-04-04 16:34
#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();
}
}
全部回答
  • 1楼网友:骨子里都是戏
  • 2021-04-04 17:06
头文件
里面定义了ifstream, ofstream,从文件中读取和写入文件东西, 自己百度下吧
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯