Reputation: 19
So, this is the question given in my book .
It is to read the following code and answere the question given below:
#include<fstream.h>
class Book
{
int Bno;char title[20];
public:
void Enterval()
{
cin>>Bno;
cin.getline(title,20);
}
void ShowVal()
{
cout<<Bno<<"#"<<title<<endl;
}
};
void Search(int Recno)
{
fstream file;
Book B;
file.open("BOOKS.DAT",ios ::binary|ios :: in);
______________// statement 1
file.read((char*)& B,sizeof(B));
B.ShowVal();
file.close();
}
1) to place the file pointer to the beginning of the desired record to be read , which is sent as parameter of the function(assuming RecNo 1 stands for the first record).
the answer given is
file.seekg((RecNo-1)*sizeof(B));
i just want to know why they've written (RecNo-1). Is the file pointer at the end of the file when it has been opened and is it pointing to the beginning of the record.
Also i want to know if the answer could be
file.seekg(-1*sizeof(B),ios::cur);
since we have to read from the beginning of the reord.
please tell me if i'm right and correct me if wrong.
Help appreciated.
Thankyou!
Upvotes: 0
Views: 3170
Reputation: 7016
The file pointer will be at the begining of a file when it is opened.
For the Nth record the statement would be
file.seekg((N-1)*sizeof(B));
This is because to view or edit the Nth record, you have to move your pointer to the end of the N-1 record and then read it, which is what the above mentioned code does.
What you have proposed
file.seekg(-1*sizeof(B),ios::cur);
Will not work as you have just opened your file and are trying to go to a position that exists before the first position in the file which is impossible, So this code will not work.
Hope this solves your questions
Upvotes: 1
Reputation: 153977
I would presume that the RecNo - 1
is because the author wants
indexing into the file to start at 1, whereas offsets into the file
start at 0. When opening the file, the initial position will be at the
start of the file. And
file.seekg( -1 * sizeof(B), ios::cur );
should fail, since it attempts to back up before the start of the file.
Having said that, there are so many things wrong with this code, it's hard to begin. Whoever wrote it does not know C++.
Upvotes: 1
Reputation: 12777
My guess is that the numbering of the Recno
argument of the Search
function is intended to be "one-based", that is the first element is identified by the number "1".
Input stream position, on the other hand, is "zero-based" (like almost everithing in C and C++), that is the first available absolute position into the stream is identified by the number "0". Moreover, if you want to read the 1-st Book
, you must start from the beginning of the file, so you should look for the position:
position := stream_first_pos + (Recno-1)*size_of_what_recno_refers_to
That is, if you want the 1-st book, i.e. Recno == 1
, your position will go to the stream_first_pos
.
The solution proposed by yourself, i.e:
file.seekg(-1*sizeof(B),ios::cur);
is not correct in that it not makes the stream point to the Recno
-th block of size sizeof(Book)
, but it just moves the current position of the stream (in your case, 0) "sizeof(B)" positions back.
Upvotes: 0
Reputation: 171
For seeking a file which contains equally sized elements two things are important to know: Where does my record start and how large is it? Both is encoded in the statement
file.seekg((RecNo-1)*sizeof(B));
Each record has the size
sizeof(B)
Since the entries are identified by the book's number starting at 1 and the first entry starts at the first position in the file (which is position 0), you have to substract 1 from each book number to get the 0-based index. The multiplication with a record's size then gives you the starting position of the given book.
The statement
file.seekg(-1*sizeof(B),ios::cur);
would cause the current file pointer to be moved one record-sized step backwards. But after opening the file the current position is 0, which already is the start of the file. Furthermore the function Search implemented this way would never use the given book number, causing it to do the same for every book number you pass.
Upvotes: 2