Reading a data file in C++

Question:
I’m trying to read a data file that I can read just find in Visual Basic, but the integers don’t seem to work when I use Visual C++. Note: I am new to C++

example recordstruct DEPTRECORD   {   int DeptID;   char DeptName[25]   };DEPTRECORD DeptRec…….ifstream is( filename, ios::nocreate );…….is.read((char*) &DeptRec, sizeof(DeptRec))cout << DeptRec.DeptID
I’ve tried it in binary and text modes and theresult is always wrong.

Note: the sizeof(DeptRec) is correct when Iuse char DeptID[2] instead of int DeptID in thestructure.

I have been trying this for days what am I doing wrong?

Answer:
I can’t be certain this is your problem but, if you are using 32-bit Windows, you should note that integers (int) are 4 bytes in C/C++ while integers are still 2 bytes in VB.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: