C++从 ifstream 读取():没有指针?

发布于 2024-12-16 17:39:04 字数 820 浏览 4 评论 0原文

假设我有一个结构体和一个包含这些结构体的二进制表示形式的文件,我将创建一个使用 ifstream::read() 访问此二进制数据的函数/方法。

这是一个示例结构:

struct MyStruct {
    int x; //Value interested in
    int y; //Value interested in
    int anotherInteger; //Not interested
    double aDouble; //Not interested
}

我如何制作该函数(我在这里称之为 readData):读取时不使用指针,或者如果需要使用指针,我将在哪里放置正确的删除?

到目前为止,我的 readData 的相关部分如下所示:

void readData(int position, int &returnX, int &returnY) {
    ifstream inFile("binaryFile.dat",ios::binary);

    MyStruct *st = new MyStruct[1];

    inFile.seekg(sizeof(MyStruct)*pos);
    inFile.read((char*) st, sizeof(MyStruct));

    returnX = st[0].x;
    returnY = st[0].y;

    //delete [] st goes here?
}

我尝试取消注释删除部分,但出现分配错误,可能是因为 x 和 y 的值指向不再存在的内容。

关于如何解决这个问题有什么想法吗?

Suppose I have a struct and a file with binary representations of those structs and I'll make a function/method that access this binary data using ifstream::read().

Here's an example struct:

struct MyStruct {
    int x; //Value interested in
    int y; //Value interested in
    int anotherInteger; //Not interested
    double aDouble; //Not interested
}

How do I make the function (I'll call it here readData) either: not using pointers when reading or, if using pointers is necessary, where would I put the proper delete?

So far, the relevant part of my readData looks like this:

void readData(int position, int &returnX, int &returnY) {
    ifstream inFile("binaryFile.dat",ios::binary);

    MyStruct *st = new MyStruct[1];

    inFile.seekg(sizeof(MyStruct)*pos);
    inFile.read((char*) st, sizeof(MyStruct));

    returnX = st[0].x;
    returnY = st[0].y;

    //delete [] st goes here?
}

I've tried uncommenting the delete part, but I get an allocation error, probably because the values of x and y are pointing to something that doesn't exist anymore.

Any ideas on how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迟到的我 2024-12-23 17:39:04

为什么不使用局部变量呢?

void readData(int position, int &returnX, int &returnY) {
    ifstream inFile("binaryFile.dat",ios::binary);
    inFile.seekg(sizeof(MyStruct)*position);

    MyStruct st;    
    inFile.read((char*) &st, sizeof(MyStruct));

    returnX = st.x;
    returnY = st.y;
}

int main() {
    int mainx, mainy;
    readData(0, mainx, mainy);
    return 0;
}

此外,参考文献不能重新定位。因此,赋值将值赋给调用函数传递的原始 intreturnXreturnY 指向局部变量。在上面的代码中,赋值更改了 mainxmainy

Why wouldn't you use a local variable?

void readData(int position, int &returnX, int &returnY) {
    ifstream inFile("binaryFile.dat",ios::binary);
    inFile.seekg(sizeof(MyStruct)*position);

    MyStruct st;    
    inFile.read((char*) &st, sizeof(MyStruct));

    returnX = st.x;
    returnY = st.y;
}

int main() {
    int mainx, mainy;
    readData(0, mainx, mainy);
    return 0;
}

Also, references cannot be re-seated. Therefore the assignment assigns the value to the origional int passed by the calling function. returnX and returnY are not pointed at the local variables. In the code above, the assignment changes mainx and mainy.

梦在深巷 2024-12-23 17:39:04

更简单的方法是使用局部变量:

void readData(int position, int &returnX, int &returnY) {
    ifstream inFile("binaryFile.dat",ios::binary);

    MyStruct st;

    inFile.seekg(sizeof(MyStruct)*position);
    inFile.read((char*)&st, sizeof(MyStruct));

    returnX = st.x;
    returnY = st.y;
}

The simpler way it's to use a local variable:

void readData(int position, int &returnX, int &returnY) {
    ifstream inFile("binaryFile.dat",ios::binary);

    MyStruct st;

    inFile.seekg(sizeof(MyStruct)*position);
    inFile.read((char*)&st, sizeof(MyStruct));

    returnX = st.x;
    returnY = st.y;
}
两人的回忆 2024-12-23 17:39:04

delete[] 没问题。如果出现错误,这并不是因为 x 和 y 的值指向不再存在的东西,因为它们的值只是整数并且不指向任何东西。

The delete[] is fine. If you get an error, it's not because the values of x and y are pointing to something that doesn't exist anymore since their values are just integers and don't point to anything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文