C ++ |文件问题

时间:2016-06-28 15:18:57

标签: c++ file

我对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行): &#34;没有匹配函数来调用&#39; std :: basic_ofstream :: open(std :: string&amp;,std :: _ Ios_Openmode)&#39; &#34;

错误图片:https://imgur.com/YVBHDzq

我可以帮忙吗?

编辑:我使用的是Codeblocks 16.01

1 个答案:

答案 0 :(得分:1)

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

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