0

In the program below I'm a little confused with these two lines -> fbin.seekp(n * recsize); & fbin.write(reinterpret_cast<char*>(&age), sizeof(int)); In the first one, why would we start writing to the file at position n * recsize instead of position 0? I would think that we would start writing at 0 and the jump to n * recsize for writing to the next block each time a write is made. And on the second line I'm confused about most of it. I've done simple type casting but what the heck is this? What is reinterpret_cast? Is it function, it doesn't have the syntax of one or is it just a built in keyword? Why are we casting as a char pointer or are we casting as a char pointer and what's the (&age) all about? Thanks.

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>

using namespace std;

int get_int(int default_value);
char name[20];

int main()
{
    char filename[81];
    int n;
    int age;
    int recsize = sizeof(name) + sizeof(int);

    cout << "Enter file name: ";
    cin.getline(filename, 80);

    // open file for binary read and write
    fstream fbin(filename, ios::binary | ios::in | ios::out);

    if(!fbin)
    {
        cout << "Could not open file " << filename;
        return -1;
    }

    //Get record number to write to
    cout << "Enter file record number: ";
    n = get_int(0);

    // get data from end user
    cout << "Enter name: ";
    cin.getline(name, 19);
    cout << "Enter age: ";
    age = get_int(0);

    // write data to the file
    fbin.seekp(n * recsize);
    fbin.write(name, 20);
    fbin.write(reinterpret_cast<char*>(&age), sizeof(int));

    return 0;
}

// get integer function
// get an int value from keyboard; return default value
// if user enters 0-length string
//
int get_int(int default_value)
{
    char s[81];

    cin.getline(s, 80);

    if(strlen(s) == 0)
        return default_value;

    return atoi(s);
}
2
Contributors
1
Reply
16
Views
3 Years
Discussion Span
Last Post by deceptikon
0

In the first one, why would we start writing to the file at position n * recsize instead of position 0?

Judging from the code, the intention is to overwrite an existing record that may or may not be the first record. If you always used position 0, you'd always overwrite the first record.

A more interesting question is what happens if you enter a record number that exceeds what's already stored in the file? :)

What is reinterpret_cast?

It's a specialized type cast. You could use an old style cast to the same effect:

fbin.write((char*)&age, sizeof age);

What reinterpret_cast says is that the purpose of the cast is to change how the bytes of an object are represented. There are a number of reasons to use a cast, and C (or older C++) only had one almighty cast syntax to do all of them. The casting operators break it down into usage with a searchable syntax so that it's easier to grep for. In human words (risking inaccuracy):

  • static_cast: Normal conversions that aren't implicit, like double to int.
  • const_cast: Adjusting const or volatile qualifiers.
  • reinterpret_cast: Change the byte representation.
  • dynamic_cast: Polymorphic casts with type checking.

Is it function, it doesn't have the syntax of one or is it just a built in keyword?

It's a keyword and an operator.

Why are we casting as a char pointer or are we casting as a char pointer and what's the (&age) all about?

In this case, read char as byte, because that's what you're doing. You're treating the int object as an "array" of bytes for writing to the file. And of course, you want the address of age rather than the value as your pointer, because the latter would be meaningless and probably crash gloriously. :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.