开发者

Writing data from a file to multidimensional array

开发者 https://www.devze.com 2023-03-10 21:30 出处:网络
Cannot load array named \'numbers\' with a data from a txt file. I\'ve tried number of combinations: fin_fout.re开发者_JAVA技巧ad((char*)(numbers[i]),number_length);

Cannot load array named 'numbers' with a data from a txt file.

I've tried number of combinations:

fin_fout.re开发者_JAVA技巧ad((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)


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:

  int number;
  fin_fout >> number;
  array[i] = number;

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:

std::vector<int> numbers;
int number_from_file;
while (fin_fout >> number_from_file)
{
    numbers.push_back(number_from_file);
}
const unsigned int NUMBERS_IN_FILE = numbers.size();

//...
std::cout << "First number from file: " << numbers[0] << "\n";
std::cout.flush();

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:

  1. Read entire file into a string.
  2. Use std::stringstream to parse the numbers from the string.

Advanced Technique 2:

  1. Open the text file as "memory mapped". You'll need platform specific functionality for this.
  2. Parse the memory for numbers.

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:

  1. Read file into a buffer.
  2. Use custom function to transform 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.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号