参考:How to read a video file using C
代码如下:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* old;
FILE* new;
old = fopen("H:\\old\\movie.mp4", "rb");
new = fopen("H:\\new\\movie.mp4", "wb");
while(1)
{
unsigned char display[1] ;
size_t bytesread = fread(display, 1, sizeof(display), old);
if (bytesread == 0)
break ;
fwrite(display, bytesread, 1, new);
}
fclose(old);
fclose(new);
}
关于这个fread( )函数,参考C library function - fread()
The C library function size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) reads data from the given stream into the array pointed to, by ptr.
ptr − This is the pointer to a block of memory with a minimum size of size*nmemb bytes.
size − This is the size in bytes of each element to be read.
nmemb − This is the number of elements, each one with a size of size bytes.
stream − This is the pointer to a FILE object that specifies an input stream.
关于返回值:
The total number of elements successfully read are returned as a size_t object, which is an integral data type. If this number differs from the nmemb parameter, then either an error had occurred or the End Of File was reached.
关于fwrite函数,参考C library function - fwrite()
The C library function size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) writes data from the array pointed to, by ptr to the given stream.
ptr − This is the pointer to the array of elements to be written.
size − This is the size in bytes of each element to be written.
nmemb − This is the number of elements, each one with a size of size bytes.
stream − This is the pointer to a FILE object that specifies an output stream.
放一个例子:
#include<stdio.h>
int main () {
FILE *fp;
char str[] = "This is tutorialspoint.com";
fp = fopen( "file.txt" , "w" );
fwrite(str , 1 , sizeof(str) , fp );
fclose(fp);
return(0);
}
这样就会生成一个名为file.txt的文件,内容为:
This is tutorialspoint.com