-2

I am trying to read a .info file as a block of data and then get certain values from that data and store them in a struct, and I don't understand the professor's example.

I have a header file that contains a struct:

#ifndef _STRUC_H
#define _STRUC_H
#define NAMELEN 51
#define ADDLEN 125
struct record 
{
  int id;
  char name[NAMELEN];
  char address[ADDLEN];
};
#endif

And i have my main function that looks like this:

int main() {
string fileName; 
ifstream inFile; 
cout << "Please enter a filename: ";
getline(cin,fileName);
inFile.open(fileName);
if(!inFile) 
{
    cerr << "Could not open: " << fileName << endl;
    return 1;
}
cout << endl; 
record rec;
inFile.read(reinterpret_cast<char *>(&rec),sizeof(rec));
while(!inFile.eof())
  {
    cout << "Id  : " << rec.id << endl;
    cout << "Name: " << rec.name << endl;
    cout << "Addr: " << rec.address << "\n" << endl;
    inFile.read(reinterpret_cast<char *>(&rec),sizeof(rec));
  }
  inFile.close(); 
  return 0;
}

What do the values for NAMELEN and ADDLEN do and How did my professor get them?

Drise
  • 3,976
  • 4
  • 34
  • 61
  • 4
    Shouldn't you ask your professor? How would we know what was going through his mind when he defined these? – Praetorian Mar 09 '18 at 21:57
  • `NAMELEN` and `ADDLEN` are simply the maximum amount of bytes `record name` and `record address` can hold. Not sure why your professor chose this for C++, though I'd understand for C. – Treyten Carey Mar 09 '18 at 21:57
  • They are constants defining the size of the name and address char arrays, which are used as strings. Effectively, they represent the maximum length of each string (plus the null terminator '\0'). The values he selected are likely arbitrary. – Daniel Waechter Mar 09 '18 at 21:57
  • 1
    @TreytenCarey *A lot* of intro C++ courses start as C with some extra features. I've even seen mid-level C++ courses that *forbid* the use of the STL, except for maybe `std::cout`. Personally, I think it's a terrible way to go about things, but I suppose you have to start somewhere. These are also the same courses that teach people `using namespace std;` is appropriate in header files. – Drise Mar 09 '18 at 22:00
  • Using `istream::read` into a structure is not wise because the compiler is allowed to insert padding bytes between fields. If the file was written using the same structure, you'll have better results. Better to read members individually. – Thomas Matthews Mar 09 '18 at 22:03
  • When I try to get a float out of my file (That's different than the one he used for this, my file should have the correct data) instead of the char for address, it doesn't return a number. Any idea why? – Kevin Youngerman Mar 09 '18 at 22:03
  • BTW, if you are reading a binary file, you should open in `binary` mode, to prevent unwanted translations, such as 0x0a to 0x0d0a. – Thomas Matthews Mar 09 '18 at 22:03
  • There are many reasons for issues with reading floats from binary file. Consider a 32 bit integer exists in the file and you want to place those 32 bits int a 32-bit floating point format (such as sign, mantissa and exponent fields). The float will not be as you expect. – Thomas Matthews Mar 09 '18 at 22:06
  • @ThomasMatthews when I read the float in, I'm getting back a hex value ( like 008FFB34). Is there any way to solve this? – Kevin Youngerman Mar 09 '18 at 22:14
  • A warning about `while(!inFile.eof())`: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) If you got this read loop from your instructor, please carefully check out whether or not you can the instructor can be corrected without damaging your grades. If they are open to correction, privately point out the error. Use the link if you have to. – user4581301 Mar 09 '18 at 22:14
  • Check the strings. If the strings make sense you probably don't have a padding issue. If they don't make sense, see what happens if you individually `read` each of the structure's members to avoid padding. – user4581301 Mar 09 '18 at 22:34

1 Answers1

0

NAMELEN and ADDLEN are constants (defined using the #define directive.

See define.

Here NAMELEN and ADDLEN stand for name length and address length.

So wherever your professor uses NAMELEN and ADDLEN replace them by their constant value i.e. 51 and 125.