在理解 C++ 时遇到问题代码。 (寻求)

发布于 2024-12-10 14:43:33 字数 1397 浏览 0 评论 0原文

#include<stdio.h>
#include<fstream.h>
class Test
{
    char name[10];
    int data;
    public:
        void getData()
        {
            cin>>name;
            cin>>data;
        }
        void display()
        {
            cout<<name<<data;
        }
        void modify()
        {
            cin>>name;
            cin>>data;
        }
};
int main()
{
    Test t1,t2,t3,t4;
//  remove("FileIO.dat");
    t1.getData();
    t2.getData();
    t3.getData();
    t4.getData();
    fstream fp1("FileIO.dat",ios::out|ios::app);
    fp1.write((char*)&t1,sizeof(t1));
    fp1.write((char*)&t2,sizeof(t2));
    fp1.write((char*)&t3,sizeof(t3));
    fp1.write((char*)&t4,sizeof(t4));
    fp1.close();
    fstream fp2("FileIO.dat",ios::in|ios::out);
    fp2.read((char*)&t1,sizeof(t1));
    fp2.read((char*)&t2,sizeof(t2));
    int pos=-1*sizeof(t2); // ****** not understanding this line
    cout<<pos;
    fp2.seekp(pos,ios::cur);
    t2.modify();
    fp2.write((char*)&t2,sizeof(t2));
    fp2.read((char*)&t3,sizeof(t3));
    fp2.read((char*)&t4,sizeof(t4));
    t1.display();
    t2.display();
    t3.display();
    t4.display();
    fp2.close();
    return 0;
}

该程序是用 Turbo C++ 编写的,它处理将对象写入文件、读回它们以及更新已写入文件的对象。

在上面的代码中,我不明白为什么 -1 乘以 sizeof object 来获取位置。 有谁请解释一下。

#include<stdio.h>
#include<fstream.h>
class Test
{
    char name[10];
    int data;
    public:
        void getData()
        {
            cin>>name;
            cin>>data;
        }
        void display()
        {
            cout<<name<<data;
        }
        void modify()
        {
            cin>>name;
            cin>>data;
        }
};
int main()
{
    Test t1,t2,t3,t4;
//  remove("FileIO.dat");
    t1.getData();
    t2.getData();
    t3.getData();
    t4.getData();
    fstream fp1("FileIO.dat",ios::out|ios::app);
    fp1.write((char*)&t1,sizeof(t1));
    fp1.write((char*)&t2,sizeof(t2));
    fp1.write((char*)&t3,sizeof(t3));
    fp1.write((char*)&t4,sizeof(t4));
    fp1.close();
    fstream fp2("FileIO.dat",ios::in|ios::out);
    fp2.read((char*)&t1,sizeof(t1));
    fp2.read((char*)&t2,sizeof(t2));
    int pos=-1*sizeof(t2); // ****** not understanding this line
    cout<<pos;
    fp2.seekp(pos,ios::cur);
    t2.modify();
    fp2.write((char*)&t2,sizeof(t2));
    fp2.read((char*)&t3,sizeof(t3));
    fp2.read((char*)&t4,sizeof(t4));
    t1.display();
    t2.display();
    t3.display();
    t4.display();
    fp2.close();
    return 0;
}

The program is written in turbo C++ and it deals with writing objects into the files and reading them back as well as updating the object that have been written into the file.

In the above code i am not understanding why -1 is multiplied by sizeof object to get the position.
Anyone please explain.

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

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

发布评论

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

评论(1

临风闻羌笛 2024-12-17 14:43:33

这是因为您的程序正在从 t1 读取数据,从 t2 读取数据,修改 t2,然后写入 t2 的内容。

当 fp2 第一次打开时,文件看起来像这样(^ 表示文件指针的当前位置):

+-----------+-----------+-----------+-----------+
|  t1 data  |  t2 data  |  t3 data  |  t4 data  |
+-----------+-----------+-----------+-----------+
^
|

读取 t1 和 t2 后,指针现在将指向 t3 的开头:

+-----------+-----------+-----------+-----------+
|  t1 data  |  t2 data  |  t3 data  |  t4 data  |
+-----------+-----------+-----------+-----------+
                        ^
                        |

现在,为了覆盖 t2 的数据,我们需要将文件指针移回 t2 的开头。那是多远的事? -1 * sizeof(t2)

+-----------+-----------+-----------+-----------+
|  t1 data  |  t2 data  |  t3 data  |  t4 data  |
+-----------+-----------+-----------+-----------+
                        ^
                        |
            <-----------+
                  |
                  This distance == sizeof(t2)

从那里,您的程序运行fp2.seekp(pos,ios::cur);。由于 pos 为负数,它会将文件指针向后移动,并且您的文件将处于以下状态:

+-----------+-----------+-----------+-----------+
|  t1 data  |  t2 data  |  t3 data  |  t4 data  |
+-----------+-----------+-----------+-----------+
            ^
            |

现在您可以覆盖 t2 的数据。

Its because your program is reading the data from t1, reading the data from t2, modifying t2, and then writing over the contents of t2.

When the fp2 is first opened, the file looks like this (^ represents the current position of the file pointer):

+-----------+-----------+-----------+-----------+
|  t1 data  |  t2 data  |  t3 data  |  t4 data  |
+-----------+-----------+-----------+-----------+
^
|

After reading t1 and t2, the pointer is now going to be pointing to the beginning of t3:

+-----------+-----------+-----------+-----------+
|  t1 data  |  t2 data  |  t3 data  |  t4 data  |
+-----------+-----------+-----------+-----------+
                        ^
                        |

Now, in order to write over t2's data, we need to move the file pointer back to the start of t2. How far back is that? -1 * sizeof(t2):

+-----------+-----------+-----------+-----------+
|  t1 data  |  t2 data  |  t3 data  |  t4 data  |
+-----------+-----------+-----------+-----------+
                        ^
                        |
            <-----------+
                  |
                  This distance == sizeof(t2)

From there, your program runs fp2.seekp(pos,ios::cur);. Since pos is negative, it moves the file pointer backwards, and your file is left in this state:

+-----------+-----------+-----------+-----------+
|  t1 data  |  t2 data  |  t3 data  |  t4 data  |
+-----------+-----------+-----------+-----------+
            ^
            |

And now you can overwrite t2's data.

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