在理解 C++ 时遇到问题代码。 (寻求)
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您的程序正在从 t1 读取数据,从 t2 读取数据,修改 t2,然后写入 t2 的内容。
当 fp2 第一次打开时,文件看起来像这样(
^
表示文件指针的当前位置):读取 t1 和 t2 后,指针现在将指向 t3 的开头:
现在,为了覆盖 t2 的数据,我们需要将文件指针移回 t2 的开头。那是多远的事?
-1 * sizeof(t2)
:从那里,您的程序运行
fp2.seekp(pos,ios::cur);
。由于 pos 为负数,它会将文件指针向后移动,并且您的文件将处于以下状态:现在您可以覆盖 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):After reading t1 and t2, the pointer is now going to be pointing to the beginning of t3:
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)
: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:And now you can overwrite t2's data.