C接口和C++接口根据文件大小开辟缓冲区并读写二进制文件示例
1.使用c的stat接口获取文件大小
2.使用C的FILE接口根据文件大小开辟缓冲区并读写二进制文件
3.使用C++的fstream接口根据文件大小开辟缓冲区并读写二进制文件
#include <assert.h>
#include <IO.h>
#include <sys/stat.h>
#include <iostream>
#include <string>
#include <fstream>
static const int INT_FILE_BYTES_MAX = 4096; //文件的最大长度
// 获取文件大小接口
long get_file_size(const char *filename)
{
struct stat f_stat;
if (stat(filename, &f_stat) == -1)
{
return -1;
}
return f_stat.st_size;
}
// C 方式读取文件
int read_file_by_c(const char* szFile, char** pOutBuf, long & nfileSize)
{
assert(NULL != szFile);
assert(NULL != pOutBuf);
nfileSize = 0;
int n_real_read_bytes = -1;
do
{
FILE* p_file = NULL;
errno_t err = ::fopen_s(&p_file, szFile, "rb");
if (0 != err)
{
::fclose(p_file);
if (0 != ::fopen_s(&p_file, szFile, "rb"))
{
printf("open file error. file=%s", szFile);
break;
}
}
::fseek(p_file, 0, SEEK_END);
long l_file_size = ftell(p_file); //返回指针偏离文件头的位置(即文件中字符个数)
if (l_file_size <= 0 || l_file_size > INT_FILE_BYTES_MAX + 64)
{
break;
}
*(pOutBuf) = new char[l_file_size];
char* p_read_buf = *(pOutBuf);
memset(p_read_buf, 0, l_file_size);
::fseek(p_file, 0, SEEK_SET);
n_real_read_bytes = (int)fread_s(p_read_buf, l_file_size * sizeof(char), sizeof(char), l_file_size, p_file);
::fclose(p_file);
nfileSize = l_file_size;
} while (false);
return n_real_read_bytes;
}
// C 方式写入
int write_file_by_c(const char* szFile, const char* pBuf, int nLen)
{
assert(NULL != szFile);
assert(NULL != pBuf);
assert(nLen > 0);
int n_real_write_bytes = -1;
do
{
FILE* p_file = NULL;
std::string str_file = szFile;
errno_t err = ::fopen_s(&p_file, str_file.c_str(), "wb+");
if (0 != err)
{
::fclose(p_file);
printf("save file error. file=%s", szFile);
break;
}
n_real_write_bytes = (int)::fwrite(pBuf, sizeof(char), nLen, p_file);
::fflush(p_file);
::fclose(p_file);
} while (false);
return n_real_write_bytes;
}
// C++ 方式读取
long long read_file_by_cpp(const char* szFile, char** pOutBuf, long long & lFileSize)
{
assert(NULL != szFile);
assert(NULL != pOutBuf);
lFileSize = -1;
do
{
std::ifstream file_in(szFile, std::ios::binary);
if (!file_in.is_open())
{
file_in.close();
if (!file_in.is_open())
{
file_in.close();
std::cout << "open file error. file=" << szFile << std::endl;
break;
}
}
file_in.seekg(0, std::ios_base::end);
std::streampos pos = file_in.tellg();
long long l_file_len = static_cast<long long>(pos);;
if (l_file_len <= 0 ||l_file_len > INT_FILE_BYTES_MAX + 64)
{
break;
}
*(pOutBuf) = new char[l_file_len];
char* p_read_buf = *(pOutBuf);
memset(p_read_buf, 0, l_file_len);
file_in.seekg(0, std::ios_base::beg);
file_in.read((char*)p_read_buf, l_file_len);
file_in.close();
lFileSize = l_file_len;
} while (false);
return lFileSize;
}
// c++ 方式写入
bool write_file_by_cpp(const char* szFile, const char* pBuf, long long nLen)
{
assert(NULL != szFile);
assert(NULL != pBuf);
assert(nLen > 0);
bool b_ret = false;
do
{
std::ofstream file_out(szFile, std::ios::binary | std::ios::trunc);
if (!file_out.is_open())
{
file_out.close();
std::cout << "save file error. file=" << szFile << std::endl;
break;
}
file_out.write((const char*)pBuf, nLen);
file_out.flush();
file_out.close();
b_ret = true;
} while (false);
return b_ret;
}
int main(int argc, char *argv[])
{
{
// test get file size
const char* sz_file = "E:\\test.dat";
long l_size = get_file_size(sz_file);
printf("file length=%ld\n", l_size);
}
{
// test c api
const char* sz_file = "E:\\test.dat";
char* p_buf = NULL;
long l_file_size = 0;
int n_read_bytes = read_file_by_c(sz_file, &p_buf, l_file_size);
const char* sz_w_file = "E:\\test_c.dat";
int n_write_bytes = write_file_by_c(sz_w_file, p_buf, n_read_bytes);
printf("file length=%ld, read length=%d, write length=%d\n", l_file_size, n_read_bytes, n_write_bytes);
}
{
// test cpp api
const char* sz_file = "E:\\test.dat";
char* p_buf = NULL;
long long l_file_size = 0;
long long l_read_bytes = read_file_by_cpp(sz_file, &p_buf, l_file_size);
const char* sz_w_file = "E:\\test_cpp.dat";
bool b_write = write_file_by_cpp(sz_w_file, p_buf, l_read_bytes);
printf("file length=%lld, read length=%lld, write ret=%d\n", l_file_size, l_read_bytes, (b_write ? 1 : 0));
}
return 0;
}
本文提供了使用C和C++接口读取和写入二进制文件的示例代码,包括获取文件大小,开辟缓冲区,并进行读写操作。示例涵盖了从文件打开、缓冲区分配到数据读写的完整流程。
1724

被折叠的 条评论
为什么被折叠?



