// MyClass.h
{
private:
char FirstName;
char LastName;
char MiddleName;
int ID;
int Age;
};
What is this struct/class? I guess that it is myClass. Anyway myClass is not meaningful name.
- FirstName/LastName/MiddleName eighter are initials - which means that the name is wrong - or are badly typed. What you are looking for is eighter std::string or char *.
- Traditionally ID is the first field - if it is needed.
``
// Globals
const int myIndex = 256;
myClass classType[ myIndex ];
As a rule of thumb - never use globals. You will have probles.
int main()
{
// assume preprocessors are included
cout << "Enter File: ";
cin >> cArray;
What is cArray?
if ( !inFile.good() )
Where is inFile defined? Why do you check IO state before any IO operation on this stream?
{
cout << "Wrong?" << endl;
}
inFile.open( cArray );
Well. Isn't simpler to write ifstream inFile(cArray)
?
while ( !inFile.eof() )
{
linecount++ // giving me 1 and not counting the file lines
You ask for overflow. What if file have more the 256 lines? What worst program will not crash - it will likely write in some unspecifed place.
inFile.read( ( char * ) &myType[linecount], sizeof( myClass ) );
- Never use binary formats unless you have to. They are very hard to debug. If you have a nice, simple text format all you need to test is a text editor to edit test input files. With binary you have no guarantee that the bug is in the program not in the test case.
Evern if you do use binary format do not read it this way. In fact you have no way to determine if the compiler haven't changed the offsets. For example ID
will have usually an offset of 4 bytes. But compiler is free to optimize it.
Additionaly you have no way of determine the size of ID
and Age
except they are bigger then 2 bytes. They usually are 4, but on some 64-bits compilers (IMHO such way is correct where int
== single word) thay may be 8. In the future thay may be 16 (if there will be 128-bit computers). You may think there never be but at the same way "768 K was enought for everybody" (that was a lot those days).
If you try to read text in such way
}
}
Unless you need to validate the input (in such case iostreams is not the best tool):
class person
{
public:
person (const std::string &first_name,
const std::string &last_name,
const std::string &middle_name,
int id,
int age)
: m_first_name(first_name),
m_last_name(last_name),
m_middle_name(middle_name,
m_id(id),
m_age(age) {}
private:
std::string m_first_name, m_last_name, m_middle_name;
int m_id, m_age;
};
// Lots of other code
std::vector<person> people;
while(...)
{
std::string first_name, last_name, middle_name;
int id, age;
in_file >> first_name >> last_name >> middle_name >> id >> age;
person p(first_name, last_name, middle_name, id, age);
people.push_back(p);
}
It can be shorten and it have to be fill but:
1. Use nice C++ features such as STL (you don't have to remember index or you don't have to worry about vector overflow)
2. It uses text format