0

I am trying to copy the contents of the binary file whose path I have specified in the code to a vector using the methods in the link, but I am getting the error in the line I added as a comment line. how can i solve this error or which method is best for me?

Link: https://sites.google.com/site/hashemian/home/tips-and-tricks/copy-array-cpp

    void MainWindow::on_horizontalScrollBar_actionTriggered(int action)
   {
   QFile read_data("C:/Users/cats_user/Desktop/00000000.log");
   char file_data;
   QByteArray fullContent;

   if(!read_data.open(QFile::ReadOnly)){
       return;
   }

   while(!read_data.atEnd()){
   read_data.read(& file_data, sizeof(char));
   fullContent.append((file_data));
   }

   std::vector<char>theVector;
   int n =sizeof (fullContent)/sizeof (char);
   theVector.insert(theVector.end(), fullContent, fullContent+n); // 'operator int' is a private member of QByteArray note: declared private here

   }
  • `fullContent, fullContent+n` whats that? You seem to think `QByteArray` is just a pointer, but it is not – 463035818_is_not_an_ai Aug 26 '22 at 08:13
  • 1
    `int n =sizeof (fullContent)/sizeof (char);` -- I don't use `qt` and I know that this will not do what you think it does. Print out the value of `sizeof(fullContent)`. – PaulMcKenzie Aug 26 '22 at 08:14
  • 1
    if the site you copied the code from had used `std::begin(fullContent)` and `std::end(fullContent)` it would work. The site is providing poor / outdated advice. Nowadays you never need `sizeof` to get the size of an array – 463035818_is_not_an_ai Aug 26 '22 at 08:15
  • 1
    Why do you use the temporary `fullContent` to begin with, instead of adding directly to the vector? – Some programmer dude Aug 26 '22 at 08:15
  • Also if you have raw binary data you should probably be using `unsigned char` (or `std::byte` or `std::uint8_t`). – Some programmer dude Aug 26 '22 at 08:16
  • @Someprogrammerdude QByteArray is a wrapper around a an array of `char` – Caleth Aug 26 '22 at 08:17
  • And there are ways to get the size of the file. With that you could resize the vector to the correct size, and read everything all at once directly into the vector. – Some programmer dude Aug 26 '22 at 08:17
  • 1
    Really sorry, but you became a victim of [cargo-cult-programming](https://en.wikipedia.org/wiki/Cargo_cult_programming). Don't use code from some online sites without understanding in-depth (!) what it actually does and why it does things the way it does. – 463035818_is_not_an_ai Aug 26 '22 at 08:17
  • @Caleth So is `std::vector` basically. I still don't see the need for `fullContent`. To be honest, I don't see the need for the vector either since the OP doesn't tell us what it's for. Perhaps the `QByteArray` could be enough for the OP's real needs? – Some programmer dude Aug 26 '22 at 08:19
  • @Someprogrammerdude exactly – Caleth Aug 26 '22 at 08:24
  • Does this answer your question? [How to read a file into vector in C++?](https://stackoverflow.com/questions/15138785/how-to-read-a-file-into-vector-in-c) – starball Aug 26 '22 at 08:42

1 Answers1

1

The site you are copying from is teaching you how to copy from a C style array. QByteArray is already a class similar to std::vector, and it interoperates with std::vector in the normal manner.

void MainWindow::on_horizontalScrollBar_actionTriggered(int action)
{
    QFile read_data("C:/Users/cats_user/Desktop/00000000.log");
    QByteArray fullContent = read_data.readAll();
    std::vector<char> theVector(fullContent.begin(), fullContent.end());     
}

You only need to copy it into a vector if you are calling code expecting exactly std::vector<char>. If the code you are calling is generic, it can use QByteArray, as that has the same members as std::vector<char> with the same meanings.

As an alternative, you could read into the vector directly.

void MainWindow::on_horizontalScrollBar_actionTriggered(int action)
{
    std::fstream read_data("C:/Users/cats_user/Desktop/00000000.log", std::ios_base::in|std::ios_base::binary);
    std::istreambuf_iterator<char> first = read_data, last;
    std::vector<char> theVector(first, last);     
}
Caleth
  • 52,200
  • 2
  • 44
  • 75
  • The file whose path I specified is binary file. I tried the examples you gave, but I could not read the content as binary. – Süha Aydın Aug 31 '22 at 12:03
  • @SühaAydın you may need to pass `std::ios_base::in|std::ios_base::binary` as the mode parameter of the [constructor](https://en.cppreference.com/w/cpp/io/basic_fstream/basic_fstream) – Caleth Aug 31 '22 at 12:25