c++ |文件问题



我对c++很陌生,我想开始处理文件,所以我最终这样做:

#include <iostream>
#include <string>
#include <cstdlib>
#include <windows.h>
#include <stdexcept>
#include <limits>
#include <Lmcons.h>
#include <fstream>
using namespace std;

void out(string x){x+="n";cout<<x;}
void outn(){out("");}
void delay(int x){Sleep(x);}
void delayS(int x){Sleep(x*1000);}
void cs(){std::system("cls");}
void UserName(string *x){char username[UNLEN + 1];DWORD size = UNLEN +  1;GetUserName(username, &size);string transition(username);*x=transition;}
//use this synthax in main :  char user[20];string    username(user);UserName(&username);
using namespace std;
int main()
{
char user[20];string username(user);UserName(&username);
out(username);
delayS(2);
cs();
string beginning="C:\Users\" ;
string path;
string ending="\Desktop\";
string filename;
out("file name = ");
cin>>filename;
path+=beginning;
out(path);
delayS(2);
path+=username;
out(path);
delayS(2);
path+=ending;
out(path);
delayS(2);
path+=filename;
out(path);
delayS(2);
ofstream file;
try{
    file.open(path ,ios::in);
    if(!file.is_open()){throw 404;}
    else{
    file.open(path,ios::out|ios::app);
    file<<"n";
    file<<"lol";}

    }catch(int x){cout<<"Error "<<x<<" : file not found";}

    return 0;
}

导致以下错误(第59行):"没有匹配的函数调用'std::basic_ofstream::open(std::string&, std::_Ios_Openmode)' "

错误图像:https://i.stack.imgur.com/D4kqb.jpg

可以帮我一下吗?

编辑:我使用Codeblocks 16.01

在c++ 11之前,您需要将const char*传递给ofstream::open()作为第一个参数:

file.open( path.c_str(), ios::in );

相关内容

  • 没有找到相关文章

最新更新