So i have a class object type, myClass classType as a global. whats not happeing is that would prefer to have
所以我有一个类对象类型,myClass classType作为全局。没有发生的事情是更愿意拥有的
// MyClass.h
{
private:
char FirstName;
char LastName;
char MiddleName;
int ID;
int Age;
};
// Globals
const int myIndex = 256;
myClass classType[ myIndex ];
int main()
{
// assume preprocessors are included
cout << "Enter File: ";
cin >> cArray;
if ( !inFile.good() )
{
cout << "Wrong?" << endl;
}
inFile.open( cArray );
while ( !inFile.eof() )
{
linecount++ // giving me 1 and not counting the file lines
inFile.read( ( char * ) &myType[linecount], sizeof( myClass ) );
}
}
Thats a rough idea of what main looks like for me today. I have yet to use myClass.cpp at all! My Text file is of this format:
这是一个粗略的想法,今天的主要外观。我还没有使用myClass.cpp!我的文本文件格式如下:
FirstName LastName ID Number Age ...
FirstName LastName ID号年龄...
After debugging, i notice that the newline character was not detected and now everything is BUNCHED in, on the same index! Like my Increment didnt work or something..
调试后,我注意到在同一个索引上没有检测到换行符,现在所有内容都被BUNCHED了!就像我的增量没有工作或什么..
I wanted to increment for however many lines their are in the file. ( more than one ) Im trying to fix the linecount ( idx ) in hopes of fully resolving this issue.
我想增加它们在文件中的许多行。 (不止一个)我试图修复linecount(idx),希望能够完全解决这个问题。
My Private members, FirstName LastName, and so on as i wrote above are present when debugging and dragging on the myclasstype. I just Need to get them with their proper varaible. I just hope the ifstream::read() function is not leading me in a bad direction.
调试和拖动myclasstype时,我上面写的我的私有成员,FirstName LastName等等存在。我只需要用适当的变量来获取它们。我只是希望ifstream :: read()函数不会导致我的方向不好。
Any Suggestions?
4 个解决方案
#1
// 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.
这个结构/类是什么?我想这是myClass。无论如何,myClass并不是有意义的名字。
- 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.
FirstName / LastName / MiddleName eighter是首字母 - 这意味着名称错误 - 或者输入错误。您正在寻找的是eighter std :: string或char *。
传统上,ID是第一个字段 - 如果需要的话。
``
// 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?
什么是cArray?
if ( !inFile.good() )
Where is inFile defined? Why do you check IO state before any IO operation on this stream?
inFile定义在哪里?为什么在此流上的任何IO操作之前检查IO状态?
{
cout << "Wrong?" << endl;
}
inFile.open( cArray );
Well. Isn't simpler to write ifstream inFile(cArray)
?
好。编写ifstream inFile(cArray)并不简单?
while ( !inFile.eof() ) { linecount++ // giving me 1 and not counting the file lines
while(!inFile.eof()){linecount ++ //给我1而不计算文件行
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.
你要求溢出。如果文件有256行以上怎么办?最糟糕的程序不会崩溃 - 它可能会写在一些不明确的地方。
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.Evern如果你使用二进制格式,请不要这样读。实际上,您无法确定编译器是否未更改偏移量。例如,ID通常具有4个字节的偏移量。但编译器可以*地优化它。
Additionaly you have no way of determine the size of
ID
andAge
except they are bigger then 2 bytes. They usually are 4, but on some 64-bits compilers (IMHO such way is correct whereint
== 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).另外,你无法确定ID和Age的大小,除非它们大于2个字节。它们通常是4,但是在一些64位编译器上(恕我直言,这样的方式是正确的,其中int ==单个单词)thay可能是8.将来thay可能是16(如果有128位计算机)。你可能认为从来没有,但以同样的方式“为所有人提供了768 K”(这些日子很多)。
除非必须,否则不要使用二进制格式。它们很难调试。如果你有一个漂亮,简单的文本格式,你需要测试的是一个文本编辑器来编辑测试输入文件。使用二进制文件,您无法保证错误是在程序中而不是在测试用例中。
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):
除非你需要验证输入(在这种情况下,iostreams不是最好的工具):
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
它可以缩短并且必须填充但是:1。使用不错的C ++功能,例如STL(你不必记住索引或者你不必担心矢量溢出)2。它使用文本格式
#2
Several points;
- what is cArray?
- you must test that the file opened using is_open()
- the eof() function will only detect end of file AFTER you have read something
- you should be using std::getline() to read lines
- as was pointed out in a comment, the names in your class are single chars, they should be std::strings
什么是cArray?
你必须测试使用is_open()打开的文件
eof()函数只会在你读完之后检测文件的结尾
你应该使用std :: getline()来读取行
正如在评论中指出的那样,你的类中的名字是单个字符,它们应该是std :: strings
#3
- I've recommended the use of boost::serialization for the same goals.
- Why do you increment linecount before reading a line? You leave the first element of myType uninitialized.
- If you're using a text file format, why are you using the read function?
我建议使用boost :: serialization来实现相同的目标。
为什么在读一行之前增加linecount?您将myType的第一个元素保留为未初始化。
如果您使用的是文本文件格式,为什么使用读取功能?
#4
You approach with read(buf, size) won't work if your data comes from a textfile. It will fill buf with whatever bytes are read from the input. You can't "fill" an object somewhere in memory that way. read(buf, size) works only with binary data.
如果您的数据来自文本文件,那么使用read(buf,size)的方法将不起作用。它将使用从输入读取的任何字节填充buf。你不能以这种方式在内存中的某个地方“填充”对象。 read(buf,size)仅适用于二进制数据。
I would overload the stream operator >> to do the actual parsing of the textdata. The output will be stored in the object passed as a reference:
我会重载流运算符>>来实际解析textdata。输出将存储在作为引用传递的对象中:
istream& operator>> (istream& in, MyClass& val );
istream&operator >>(istream&in,MyClass&val);
#1
// 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.
这个结构/类是什么?我想这是myClass。无论如何,myClass并不是有意义的名字。
- 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.
FirstName / LastName / MiddleName eighter是首字母 - 这意味着名称错误 - 或者输入错误。您正在寻找的是eighter std :: string或char *。
传统上,ID是第一个字段 - 如果需要的话。
``
// 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?
什么是cArray?
if ( !inFile.good() )
Where is inFile defined? Why do you check IO state before any IO operation on this stream?
inFile定义在哪里?为什么在此流上的任何IO操作之前检查IO状态?
{
cout << "Wrong?" << endl;
}
inFile.open( cArray );
Well. Isn't simpler to write ifstream inFile(cArray)
?
好。编写ifstream inFile(cArray)并不简单?
while ( !inFile.eof() ) { linecount++ // giving me 1 and not counting the file lines
while(!inFile.eof()){linecount ++ //给我1而不计算文件行
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.
你要求溢出。如果文件有256行以上怎么办?最糟糕的程序不会崩溃 - 它可能会写在一些不明确的地方。
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.Evern如果你使用二进制格式,请不要这样读。实际上,您无法确定编译器是否未更改偏移量。例如,ID通常具有4个字节的偏移量。但编译器可以*地优化它。
Additionaly you have no way of determine the size of
ID
andAge
except they are bigger then 2 bytes. They usually are 4, but on some 64-bits compilers (IMHO such way is correct whereint
== 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).另外,你无法确定ID和Age的大小,除非它们大于2个字节。它们通常是4,但是在一些64位编译器上(恕我直言,这样的方式是正确的,其中int ==单个单词)thay可能是8.将来thay可能是16(如果有128位计算机)。你可能认为从来没有,但以同样的方式“为所有人提供了768 K”(这些日子很多)。
除非必须,否则不要使用二进制格式。它们很难调试。如果你有一个漂亮,简单的文本格式,你需要测试的是一个文本编辑器来编辑测试输入文件。使用二进制文件,您无法保证错误是在程序中而不是在测试用例中。
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):
除非你需要验证输入(在这种情况下,iostreams不是最好的工具):
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
它可以缩短并且必须填充但是:1。使用不错的C ++功能,例如STL(你不必记住索引或者你不必担心矢量溢出)2。它使用文本格式
#2
Several points;
- what is cArray?
- you must test that the file opened using is_open()
- the eof() function will only detect end of file AFTER you have read something
- you should be using std::getline() to read lines
- as was pointed out in a comment, the names in your class are single chars, they should be std::strings
什么是cArray?
你必须测试使用is_open()打开的文件
eof()函数只会在你读完之后检测文件的结尾
你应该使用std :: getline()来读取行
正如在评论中指出的那样,你的类中的名字是单个字符,它们应该是std :: strings
#3
- I've recommended the use of boost::serialization for the same goals.
- Why do you increment linecount before reading a line? You leave the first element of myType uninitialized.
- If you're using a text file format, why are you using the read function?
我建议使用boost :: serialization来实现相同的目标。
为什么在读一行之前增加linecount?您将myType的第一个元素保留为未初始化。
如果您使用的是文本文件格式,为什么使用读取功能?
#4
You approach with read(buf, size) won't work if your data comes from a textfile. It will fill buf with whatever bytes are read from the input. You can't "fill" an object somewhere in memory that way. read(buf, size) works only with binary data.
如果您的数据来自文本文件,那么使用read(buf,size)的方法将不起作用。它将使用从输入读取的任何字节填充buf。你不能以这种方式在内存中的某个地方“填充”对象。 read(buf,size)仅适用于二进制数据。
I would overload the stream operator >> to do the actual parsing of the textdata. The output will be stored in the object passed as a reference:
我会重载流运算符>>来实际解析textdata。输出将存储在作为引用传递的对象中:
istream& operator>> (istream& in, MyClass& val );
istream&operator >>(istream&in,MyClass&val);