【发布时间】:2015-12-14 21:27:38
【问题描述】:
我有两个文件,其中一个我创建了简单的类:
#include <iostream>
#include <fstream>
#include <string>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include <unistd.h>
class myclass{
protected:
int ima,imb,imc,tm;
fstream file;
public:
void creafile(string filename){
string dir;
dir = "txtfile/";
file.open((dir + filename).c_str(), ios::in | ios::out);
if(file.fail()){
// file.open(filename, ios::in | ios::out);
//if(file.fail())
cout<<"Error when creating the file"<<endl;
exit(1);
}
file.close();
}}
我的主文件名为 data.cpp,只包含以下代码:
using namespace std;
#include "mylib.h"
int main() {
myclass dat,hi;
dat.creafile("creatorfile.txt");
return 0;
}
我的问题是调用creafile时总是出错创建文件时出错。为了做一个更简单的测试用例,我还尝试了以下代码:
file.open("myfile.txt");
if(!file){
cout<<"Error when creating the file"<<endl;
exit(1);
}
file.close();
但是,它仍然给出错误创建文件时出错。我尝试使用所有标志 ios::app ios::in ios::out 等,但没有任何变化。我有 500gb 的可用空间,并且正在运行 Windows 7。
【问题讨论】:
-
也许是一个小小的疏忽,也许您需要以管理员身份运行程序?有可能。
-
我什么都试过了..没用..奇怪的问题
-
为你的目录尝试 ../txtfile/
-
不,我的 data.cpp 文件在 dir 信息文件夹和我的类文件中。 txtfile文件夹也是