简体   繁体   中英

How to read .mp4 file as bytes in c++ and store it in an array

I want to read a .mp4 file as bytes in c++. Finally I need to store to read file as a byte array and send it across a udp socket. This is the code that I have written. The sending code seems fine to me but the file is not getting read properly. Can someone please help.

#include <iostream>
#include <bits/stdc++.h>
#include "VideoPacket.h"
#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

class VideoPacket {
public:
    bool flag;
    string filename;
    int packetNum;
    int index;
    char *data;
    time_t timestamp{};

    explicit VideoPacket(bool p_flag, string &p_filename, int p_packetNum, char *p_data, int p_index) {
        flag = p_flag;
        packetNum = p_packetNum;
        index = p_index;
        data = p_data;
        filename = p_filename;
    }
};

vector<VideoPacket> readFile(string &filename) {
    ifstream fin(filename);
    vector<VideoPacket> videoData;
    char buff[4096];

    int counter = 0;
    do {
        fin.read(buff, sizeof(buff));
        cout << buff << endl;
        VideoPacket newPacket(true, filename, -1, buff, counter);
        counter++;
        videoData.push_back(newPacket);
    } while (!fin.eof());
    return videoData;
}

int main() {
    string filename = R"(videoSrc)";
    vector<VideoPacket> videoData = readFile(filename);
    return 0;
}

The issue I am facing is that file is not getting read properly. The while loop for reading the file only run once but the file size is 208kb.

Any help is appreciated. Thanks

There are multiple logical flaws here.

char buff[4096];

We begin by declaring this array. So far, so good.

        fin.read(buff, sizeof(buff));

read() reads the next chunk of (hopefully) 4096 bytes. Still doing great.

        cout << buff << endl;

This is where things go south. This calls an operator<< that takes a pointer to a char as its parameter. Additionally, this << expects to get a pointer to a C-style string here, with zero or more non-0 bytes, followed by a single 0 byte. That's the string that'll be printed. It is almost a certainty that somewhere in those 4096 bytes, given that this is an MP4 file, there's going to be a whole bunch of 0 bytes there. The first one will do. That's going to be your output, right there. Your output will quickly finish, much sooner than you expected.

And if there just aren't any 0 bytes, in any of those read() 4096 bytes, this is going to blow past those 4096 bytes, looking for the trailing 0 byte. You may get all sorts of interesting output, like your vacation pictures, maybe even your Facebook password, etc... It's all going to go to std::cout .

} while (!fin.eof());

And here things can't get any worse than this. eof() only returns true after a read operation fails because the end of file was reached. If you just happen to have read the last 4096 bytes of your MP4 file, eof() is still false, so you'll go back to the top, and attempt to read 4096 bytes more. That'll fail. But the shown code is blissfully unaware, it has no idea that this has happened. So, it will attempt to dump the contents of buf , which will be exactly the same, and this will dump another random amount of garbage to std::cout .

And if the MP4 file's size was not an even multiple of 4096, the last read() operation produces partial data, but everything else in the loop still expects that exactly 4096 bytes were read. The overall logic for reading the file is fundamentally flawed.

In conclusion, the following must be fixed:

  1. The logic for the output to std::cout is flawed, and should be fixed.

  2. The logic for reading from the file is flawed, and must be fixed. gcount() returns the number of characters that read() actually read. The shown code must use that, both for handling the read input, and for end-of-file detection ( gcount() will return 0).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM