devxlogo

Reading a data file in C++

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.

See also  Does It Make Sense to Splurge on a Laptop?
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist