C ++无法从可变长度的缓冲区写入文件

C++ Unable to write to file from buffer of variable length

我想做的是:

  1. 在 C++ 中打开 .264。
  2. 获取文件大小。
  3. 以 128kb 的块读取文件(缓冲区明智)。
  4. 在 128kb 的块中,搜索特定的十六进制序列(header 信息)。
  5. 获取帧大小(它位于 header 开头的第 12-15 个位置)。
  6. 将等于帧大小的内容复制到另一个动态缓冲区(大小等于帧大小)。
  7. 从 (6) 的动态缓冲区写入输出文件。

我面临两个问题;一个不成功的文件写入和我的动态缓冲区的内容没有 change.Can 有人解释可能是什么地方错了吗?

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sstream>
#include <iostream>
#include <math.h>
#include <fstream>
#include <stdint.h>

char const* inputFileName = "file15.264";
int file_open = open(inputFileName, O_RDWR);
//==================Global declaration of buffer===========================
static uint8_t buffer[131072];

//=========================================================================
//===========Snippet to calculate FileSize=================================
std::ifstream::pos_type filesize(const char* filename)
{
    std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
    return in.tellg(); 
}
//===========================================================================

//==============================MAIN FUNCTION================================
int main(int argc, char** argv) 
{

    int input_size = filesize(inputFileName);
    float in_size_mb = input_size/(1024*1024);

    printf("Size of input File: %d bytes(%f Megabytes).\n\n",input_size,in_size_mb);

    int output = open("output_15.264", O_RDWR|O_CREAT);

    int bytes_read = 0;
    int foundAt = -1; //initializer for foundAt
    int index = 0;    
    unsigned int frame_size;
    static uint8_t * frame_buffer; 
    unsigned int array_size;
    while (bytes_read < input_size) 
    {   
        read(file_open,buffer,131072);
            while (index < 131072-8+1 )/* buffer has size 128Kb and hex seq to search is of size 8bytes */
            {
                if( 
                    (buffer[index + 0] == 0x00)&& 
                    (buffer[index + 1] == 0x00)&&
                    (buffer[index + 2] == 0x00)&&
                    (buffer[index + 3] == 0x00)&&
                    (buffer[index + 4] == 0x00)&&
                    (buffer[index + 5] == 0x00)&&
                    (buffer[index + 6] == 0x00)&&
                    (buffer[index + 7] == 0xab) )
                    {
                        foundAt = index;
                        printf("first byte in the sequence is located at index: %d\n",foundAt);
                        printf("Starting Address of Data: 0x%X\n",&buffer[index+32]);
                        //Bitwise Shift hex from buf[12] to buf[15] to get frame size  
                        frame_size = (  (buffer[index+12]<< 24)|
                                        (buffer[index+13]<< 16)|
                                        (buffer[index+14]<< 8 )|
                                         buffer[index+15] );
                        printf("Size of Following Frame: 0x%X\n",frame_size);
                        //write(output,(&buffer+32),frame_size);
                        array_size = frame_size;
                        frame_buffer = (uint8_t*)malloc(array_size );//*sizeof(int));
                        printf("Size of Small buffer(frame_buffer):%zu Bytes & Contents' Size: %d\n",sizeof(frame_buffer),array_size);
                        memcpy(frame_buffer,buffer+(index+32),array_size);

                        printf("frame buffer[33]: %x [42]: %x [50]: %x\n\n\n",frame_buffer[1],frame_buffer      [2],frame_buffer[3]);
                        //sleep(3);
                        write(output,frame_buffer,frame_size);                           
                        index += frame_size;

                }
                else
                {
                    index++;
                }   


        }// END OF INTERNAL WHILE LOOP THAT SEARCHES FOR HEADER START
    //write(output,buffer+32,frame_size);
    index = 0 ;

    bytes_read += 131072;
    }//END OF OUTER WHILE LOOP THAT SCANS THE WHOLE I/P FILE HAVING FD 'FILE_OPEN'
    float j = bytes_read/(1024*1024); //just to return in mb the no of bytes read.
    printf("Total Bytes Read: %d Bytes (%f MB)\n",bytes_read,j);
    if(bytes_read == input_size)
    {
        printf("Copying Successful! \n");
    }
    else 
    {
        printf("Error: Copy not Successful!\n");
    }

    close(output);
    close(file_open);

    return 0;
}

这一行:

memcpy(frame_buffer,buffer+32,array_size);

应该是

memcpy(frame_buffer,buffer+(index+32),array_size);

否则无论您在哪里找到 header.

,您都只是从缓冲区的开头复制加上 32 个字节

您可以这样处理 header 超出当前 128Kb 块的情况:首先将缓冲区大小加倍:

static uint8_t buffer[262144];

然后,如果有溢出,则将另一个块读入数组的顶部,并在读出跨越两个块的部分后将其向下复制到下半部分:

int overflow = 0;
if((buffer+(index+32)+array_size) >= 131072)
{
    // read an extra block:
    read(file_open,buffer+131072,131072);
    overflow = 1;
}
memcpy(frame_buffer,buffer+(index+32),array_size);
if(overflow != 0)
{        
    memcpy(buffer,buffer+131072,131072);
    index -= 131072;
}

它不是很优雅,但应该可以解决问题。它假定 array_size 永远不会大于 131072-32。