1

I'm still new to advance file and structures. I'm just having trouble figuring out how to seek and read specific records from a file. and how to display the information of a specific record through it's record number?

    #include <iostream>
    #include <fstream>
    using namespace std;

struct catalog
{
  char title[50];
  char author[50];
  char publisher[30];
  int yearpublish;
  double price;
};

void showRec(catalog);
long byteNum(int);

int main()
{
  catalog book;

  fstream fbook("book.dat",ios::in|ios::binary);

  if (!fbook)
  {
    cout <<"Error opening file";
    return 0;
  }

  //Seek and read record 2 (the third record)


  showRec(book);  // Display record 2

  //Seek and read record 0 (the first record)


  showRec(book); // Display record 0

  fbook.close();
  return 0;
}

 void showRec(catalog record)
 {
    cout << "Title:" << record.title << endl;
    cout << "Author:" << record.author << endl;
    cout << "Publisher name:" << record.publisher << endl;
    cout << "Year publish:" << record.yearpublish << endl;
    cout << "Price:" << record.price << endl << endl;
 }

long byteNum(int recNum)
{
 return sizeof(catalog) * recNum;
 }

Please Help. Thank you.

user3544721
  • 71
  • 1
  • 9

1 Answers1

1

The C++ istream methods you need for this are seekg and read.

// Seek and read record 2
fbook.seekg(byteNum(2));
fbook.read((char*)&book, sizeof(book));
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Which of course, doesn't work unless `book` is an already formatted array of `char`. – James Kanze Apr 17 '14 at 11:26
  • it doesn't read the file properly though. it's all symbols displaying. – user3544721 Apr 17 '14 at 11:29
  • It doesn't solve anything. You can't just read bits from the disk and expect to get anything useful. – James Kanze Apr 17 '14 at 11:29
  • If they were written in a similar binary fashion, it should work. – Barmar Apr 17 '14 at 11:29
  • If they are written by the same program, compiled with the same compiler, using the same options, running on the same machine, perhaps. In general, however, no. About the only thing this sort of reading and writing is useful for is spilling very large data sets to disk, to be read back in the same process. – James Kanze Apr 17 '14 at 11:31
  • @user3544721 If this doesn't work, then it doesn't sound like the file was written as binary structures. You need to be more precise about the actual file format. – Barmar Apr 17 '14 at 11:31
  • @barmar tq it does work. what if i want the program to display the information of a specific record through its record number? – user3544721 Apr 17 '14 at 11:32
  • @JamesKanze It's not really that bad. The system's ABI puts constraints on how structures will be formatted, because otherwise applications, libraries, and kernels would all have to be compiled with the same implementation and options. – Barmar Apr 17 '14 at 11:33
  • @user3544721 That's the argument to `byteNum`. You wrote that, don't you understand what it does? – Barmar Apr 17 '14 at 11:33
  • @Barmar Which no doubt explains why the byte order of a long once changed from one release to the next of Microsoft C. – James Kanze Apr 17 '14 at 12:59