本文介绍了如何使用fstream指针对文件进行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用某种抽象对流进行操作,因此我想使用fstream *代替ifstream和ofstream.我试图这样做,但是会导致访问冲突:

I want to operate on streams using some abstraction and thus I want to use fstream* instead of ifstream and ofstream. I tried to do sth like that but is wil cause access violation:

char* text= "test";
fstream* o = new fstream();
o = &fstream("name.txt");
o->write(text, 4);
o->close();

如何解决它,或使用其他想法?

How can I fix it, or use another idea ?

在这种情况下,我想使用指针(您可以在此处查看更多常规信息)

I want to use pointer in this case (You can look here for more general information) How to implement my own IO file API in C++

更改后,现在看起来像这样:

After the changes it now looks like this:

class GIO_Persistent_File_System : public GIO_CORE
{
public:
GIO_Persistent_File_System(void);
int open(char*, int);
int close();
void write(char* s, int size);
void read(char* s, int size);
public:
~GIO_Persistent_File_System(void);

private:
fstream file;
};

int GIO_Persistent_File_System::open(char* path, int mode){
file.open(path);
return 0;
}

int GIO_Persistent_File_System::close(){
file.close();
return 0;
}

void GIO_Persistent_File_System::write(char* s, int size){
file.write(s, size);
return;
}

void GIO_Persistent_File_System::read(char* s, int size){
file.read(s, size);
return;
}

主要:

GIO_CORE* plik = new GIO_Persistent_File_System();
char* test = new char[10];
char* napis = "testgs";
plik->open("name.txt", OPEN_MODE);
plik->write(napis, 2);
//plik->read(test,2);
plik->close();

此代码似乎可以正常工作,我找不到文件.我检查并正确指向了当前目录(ProjectName/Debug)

And this code seems like working altough I cannot find the file. I checked and the current directory is pointed correctly (ProjectName/Debug)

我检查了它,并且将fstream更改为ofstream可以正常工作,并且可以找到该文件.但是,由于我想实现某种程度的抽象,并且我想使用fstream .我该如何解决?

I checked it and changing fstream to ofstream will works as it should and I can find the file. But since I want to achieve some level of abstraction and I would like to use fstream. How can I fix it ?

推荐答案

此代码将给您一个错误,因为您不能像使用&fstream("name.txt")那样获取临时对象的地址.

This code will give you an error, since you can't take the address of a temporary object, as you are doing with &fstream("name.txt").

error: taking address of temporary

此外,请注意,从字符串文字到char*的转换已被弃用,并且在C ++ 11中无效.使用const char*代替:

Also, note that the conversion from a string literal to a char* has been deprecated and is invalid in C++11. Use a const char* instead:

const char* text = "test";

不过,让我们看看您正在尝试做些什么.首先,您正在动态分配fstream并初始化指向该对象的指针:

Nonetheless, let's look at what you're trying to do. Firstly, you are dynamically allocating an fstream and initialising a pointer to that object:

fstream* o = new fstream();

然后在下一行中,用fstream("name.txt")创建一个临时对象,然后获取其地址并将其分配给o(如我们所见,这会产生错误).现在,您将失去对动态分配的fstream的任何访问权,而使o指向现已破坏的临时对象.

Then in the next line, you create a temporary object with fstream("name.txt") and then take its address and assign it to o (which gives an error, as we've seen). Now you would have lost any access to the dynamically allocated fstream and instead have o pointing at a now destroyed temporary object.

取消引用该指针(使用o->)会给您带来不确定的行为.

Dereferencing that pointer (with o->) will give you undefined behaviour.

您过于复杂了.您完全不需要动态分配fstream对象或使用指针.相反,请尝试:

You are overcomplicating this. You do not need to dynamically allocate your fstream object or use pointers at all. Instead, try:

fstream o("name.txt");
o.write(text, 4);
o.close();


使用更新的代码,问题在于您正在写入0个字节:


With your updated code, the problem is that you are writing 0 bytes:

plik->write(napis, 0);

也许你是说:

plik->write(napis, 6);

这篇关于如何使用fstream指针对文件进行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 21:12