无法使用 fstream::open 创建 txt 文件
Cannot create a txt file using fstream::open
我有两个文件,其中一个是我创建的简单 class :
#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 免费 space 和 运行 Windows 7.
根据the reference,ios::in | ios::out
std::ios_base::openmode
配置如果文件不存在会产生错误,所以你不会用那个创建一个新的。
我不知道你为什么要使用成员 std::fstream
,createfile
可能只是一个不改变任何对象的 static
函数。你甚至在事后关闭它!它将使用 local std::ofstream
创建文件,其打开模式为 std::ios_base::out
, 将 创建文件:
std::ofstream ofs(dir + filename); // .c_str() not needed since C++11
要点1:如果文件不存在则无法打开阅读。幸运的是,您可能不想这样做。同时读写同一个文件是有问题的,而且几乎总是一个坏主意。直到你知道你必须同时阅读和写作,
- 打开文件进行阅读
- 读入文件
- 关闭文件。
- 编辑内存中的文件
- 打开文件进行写入
- 写出文件
- 关闭文件
如果您有一个非常大的文件,您无法将其存储在内存中,
- 打开文件进行阅读
- 打开一个临时文件进行写入
- 读入部分文件
- 编辑您阅读的部分
- 把读到的部分写到临时
- 如果有更多文件,转到 3(但不要使用
goto
),否则继续
- 关闭文件
- 关闭临时文件
- 删除文件
- 将临时文件重命名为文件
要点2:你已经创建了txtfile文件夹,但是你创建的位置对了吗?您的开发环境(包括 conio.h 建议 Visual Studio 或古董)可能不是 运行 您认为是 运行.
的程序
将此添加到您的主要代码中:
char buf[4097]; // really big buffer
getcwd(buf, (int)sizeof(buf)); // get working directory
std::cout << buf << std::endl; // print the working directory
如果打印出的文件夹不是您制作txt文件夹的地方,您将无法打开该文件。如果您想自动创建文件夹,请阅读此处:How to make a folder/directory
第3点:exit(1);
真是一把大锤子。这是一个讨厌的锤子。 Read more here. 没有非常非常好的理由不要使用它。在这种情况下 return
足以让您退出函数,如果您向函数添加 return 值,main
可以测试 return 值以看看它是否应该继续或 return。或者你可以抛出异常。
我有两个文件,其中一个是我创建的简单 class :
#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 免费 space 和 运行 Windows 7.
根据the reference,ios::in | ios::out
std::ios_base::openmode
配置如果文件不存在会产生错误,所以你不会用那个创建一个新的。
我不知道你为什么要使用成员 std::fstream
,createfile
可能只是一个不改变任何对象的 static
函数。你甚至在事后关闭它!它将使用 local std::ofstream
创建文件,其打开模式为 std::ios_base::out
, 将 创建文件:
std::ofstream ofs(dir + filename); // .c_str() not needed since C++11
要点1:如果文件不存在则无法打开阅读。幸运的是,您可能不想这样做。同时读写同一个文件是有问题的,而且几乎总是一个坏主意。直到你知道你必须同时阅读和写作,
- 打开文件进行阅读
- 读入文件
- 关闭文件。
- 编辑内存中的文件
- 打开文件进行写入
- 写出文件
- 关闭文件
如果您有一个非常大的文件,您无法将其存储在内存中,
- 打开文件进行阅读
- 打开一个临时文件进行写入
- 读入部分文件
- 编辑您阅读的部分
- 把读到的部分写到临时
- 如果有更多文件,转到 3(但不要使用
goto
),否则继续 - 关闭文件
- 关闭临时文件
- 删除文件
- 将临时文件重命名为文件
要点2:你已经创建了txtfile文件夹,但是你创建的位置对了吗?您的开发环境(包括 conio.h 建议 Visual Studio 或古董)可能不是 运行 您认为是 运行.
的程序将此添加到您的主要代码中:
char buf[4097]; // really big buffer
getcwd(buf, (int)sizeof(buf)); // get working directory
std::cout << buf << std::endl; // print the working directory
如果打印出的文件夹不是您制作txt文件夹的地方,您将无法打开该文件。如果您想自动创建文件夹,请阅读此处:How to make a folder/directory
第3点:exit(1);
真是一把大锤子。这是一个讨厌的锤子。 Read more here. 没有非常非常好的理由不要使用它。在这种情况下 return
足以让您退出函数,如果您向函数添加 return 值,main
可以测试 return 值以看看它是否应该继续或 return。或者你可以抛出异常。