将文件中的数据写入多维数组
无法使用 txt 文件中的数据加载名为“numbers”的数组。
我尝试了多种组合:
fin_fout.read((char*)(numbers[i]),number_length);
fin_fout.read((char*)(&numbers[i]),number_length);
fin_fout.read((char*)(numbers[i][0]),number_length);
fin_fout.read((char*)(&numbers[i][0]),number_length);
它们都不适合我。我做错了什么?
class Reader
{
public://change this to private
static const unsigned numbers_in_file = 200;
static const unsigned number_length = 100;
static char numbers[numbers_in_file][number_length];
static std::fstream fin_fout;
static
inline
void read_unsafe_()
{
for (unsigned i = 0; !fin_fout.eof();++i)
{
fin_fout.read((char*)(numbers[i]),number_length);
}
fin_fout.close();
}
}
/*this stream will be reading and writing to a file*/
std::fstream Reader::fin_fout("data.txt",
std::ios_base::in | std::ios_base::out | std::ios_base::binary);
错误 1 错误 C2679:二进制“=”:找不到采用“char [100]”类型右侧操作数的运算符(或者没有可接受的转换)
Cannot load array named 'numbers' with a data from a txt file.
I've tried number of combinations:
fin_fout.read((char*)(numbers[i]),number_length);
fin_fout.read((char*)(&numbers[i]),number_length);
fin_fout.read((char*)(numbers[i][0]),number_length);
fin_fout.read((char*)(&numbers[i][0]),number_length);
None of them will work for me. What am I doing wrong?
class Reader
{
public://change this to private
static const unsigned numbers_in_file = 200;
static const unsigned number_length = 100;
static char numbers[numbers_in_file][number_length];
static std::fstream fin_fout;
static
inline
void read_unsafe_()
{
for (unsigned i = 0; !fin_fout.eof();++i)
{
fin_fout.read((char*)(numbers[i]),number_length);
}
fin_fout.close();
}
}
/*this stream will be reading and writing to a file*/
std::fstream Reader::fin_fout("data.txt",
std::ios_base::in | std::ios_base::out | std::ios_base::binary);
Error 1 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'char [100]' (or there is no acceptable conversion)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您的数字位于文本文件中,因此我规定它们以文本形式表示,例如“124”,即字符“1”、“2”、“4”。
我的建议是,在将这些文本表示形式存储到数组中之前,将它们转换为内部表示形式:
将数字存储为文本的问题之一是它们是变量字段。文本表示“5”包含的字符少于“31415264”。当您分配二维数组时,您必须为尽可能长的文本表示分配足够的空间。
更好的方法是从文件中读取数据,转换为数字,存储为向量,然后重复直到 EOF。 std::vector 是用于此目的的一个很好的容器,因为它可以根据需要进行扩展,这是处理数据文件时所必需的(特别是当无法保证文件中的数据量时)。
尝试这样的事情:
另外,关于性能:从文件读取时不能应用“每一纳秒都很重要”。 文件读取性能不受程序控制,取决于操作系统。如果文件 I/O 性能存在问题,请将整个文件读入内存,然后从内存中解析数字。仅当性能至关重要时才建议高级程序员使用此技术。与实现较慢、较简单的流程相比,您将花费更多的开发时间来使复杂的流程正常工作。
编辑1:
高级技术1:
std::stringstream
解析字符串中的数字。
高级技巧2:
映射”。你需要平台
为此的特定功能。
高级技巧3:
为多线程设计您的程序。一个线程使用简单的技术读取一个数字,并将其放入循环队列中。该线程设置一个信号,指示号码已准备好。另一个“处理”线程在信号上休眠。当信号被激活时,处理线程从队列中获取数字并进行处理。在网络上搜索“双缓冲”。
高级技术4:
文本表示到内部
数字表示。
您可能会在此获得一些超过技术 1 和 2 的性能。上面 2 是因为您可以仅针对数字定制自定义函数。
Since your numbers are in a text file, I'm stipulating they are represented as text, such as "124", which would be the characters '1', '2', '4'.
My recommendation is that you translate these textual representations into internal representations before storing them into an array:
One of the problems with storing the numbers as text, is that they are a variable field. The textual representation "5" contains less characters than "31415264". When you allocate a 2D array, you will have to allocate enough space for the longest possible textual representation.
The better method is to read the data from the file, convert to a number, store into a vector, then repeat until EOF. The
std::vector
is a great container for this purpose because it expands as necessary, which is required when handling data files (especially when there is no guarantee about the quantity of data in the file).Try something like this:
Also, about performance: "every nanosecond counts" cannot apply when reading from a file. File reading performance is out of the program's control and dependent on the OS. If file I/O performance is an issue, read the entire file into memory, then parse the numbers from memory. This technique is only recommended to advanced programmers and only when performance is critical. You'll spend more development time getting a complex process to work correctly than implementing a slower, simpler process.
Edit 1:
Advanced Technique 1:
std::stringstream
to parse thenumbers from the string.
Advanced Technique 2:
mapped". You'll need platform
specific functionality for this.
Advanced Technique 3:
Design your program for multiple threads. One thread reads a number, using the simple technique, and places it into a circular queue. This thread sets a signal indicating a number is ready. Another "processing" thread sleeps on the signal. When the signal is activated, the processing thread fetches the number from the queue and processes it. Search the web for "double buffering".
Advanced Technique 4:
textual representation into internal
numeric representation.
You may gain some performance here over Techniques 1 & 2 above because you can tailor the custom function for only numbers.