zoukankan      html  css  js  c++  java
  • 读写文件

    1.读文件

    FILE *fp_in = NULL;
    fp_in = fopen("test.h264", "rb");
    if (!fp_in) 
    {
        printf("Could not open input stream
    ");
        return -1;
    }
    
    unsigned char *in_buffer;
    in_buffer = new unsigned char[4096 * 4];
    
    unsigned char *cur_ptr = NULL;
    int cur_size = 0;
    
    while (feof(fp_in) == 0)
    {
        cur_size = fread(in_buffer, 1, 4096 * 4, fp_in);
        if (cur_size == 0)
            break;
    
        cur_ptr = in_buffer;
    
        //input_stream_data(cur_ptr, cur_size) ;
    
        Sleep(10);
    }
    
    fclose(fp_in);
    
    delete[] in_buffer;
    in_buffer = NULL;

    2.写文件

    写文件主要为以下:

    FILE * outfile;
    outfile = fopen("test.h264", "wb" );
    if( outfile == NULL )
    {
        printf("fopen error");
    }   
    
    fwrite( cur_ptr, sizeof( unsigned char ), cur_size, outfile );
        
    fclose(outfile);

    结合1、2,读写文件一起:

    FILE *fp_in = NULL;
    fp_in = fopen("test.h264", "rb");
    if (!fp_in) 
    {
        printf("Could not open input stream
    ");
        return -1;
    }
    
    FILE * outfile;
    outfile = fopen("out.h264", "wb" );
    if( outfile == NULL )
    {
        printf("fopen error");
    } 
    
    unsigned char *in_buffer;
    in_buffer = new unsigned char[4096 * 4];
    
    unsigned char *cur_ptr = NULL;
    int cur_size = 0;
    
    while (feof(fp_in) == 0)
    {
        cur_size = fread(in_buffer, 1, 4096 * 4, fp_in);
        if (cur_size == 0)
            break;
    
        cur_ptr = in_buffer;
    
        fwrite( cur_ptr, sizeof( unsigned char ), cur_size, outfile );
    
        Sleep(10);
    }
    
    fclose(fp_in);
    fclose(outfile);
    
    delete[] in_buffer;
    in_buffer = NULL;
  • 相关阅读:
    UVA 1386 Cellular Automaton
    ZOJ 3331 Process the Tasks
    CodeForces 650B Image Preview
    CodeForces 650A Watchmen
    CodeForces 651B Beautiful Paintings
    CodeForces 651A Joysticks
    HUST 1601 Shepherd
    HUST 1602 Substring
    HUST 1600 Lucky Numbers
    POJ 3991 Seinfeld
  • 原文地址:https://www.cnblogs.com/betterwgo/p/7894644.html
Copyright © 2011-2022 走看看