Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
FileOp.h50 linesDownload Raw Back to c_util
1#include <cstdint>
2#include <fstream>
3#include <vector>
4#include <iostream>
5
6bool WriteBinaryFile(const char* pFileName, const std::vector<uint8_t>& data)
7{
8    FILE* fp = fopen(pFileName, "wb");
9    if (fp)
10    {
11        if (data.size())
12        {
13            fwrite(&data[0], 1, data.size(), fp);
14        }
15        fclose(fp);
16    }
17    else
18    {
19        std::cout << "ERROR!! Failed to open " << pFileName << "\n";
20        std::cout << "Make sure the file or directory has write access\n";
21        return false;
22    }
23    return true;
24}
25
26bool ReadBinaryFile(const char* pFileName, std::vector<uint8_t>& image)
27{
28    FILE* fp = fopen(pFileName, "rb");
29    if (!fp)
30    {
31        std::cout << "ERROR!! Failed to open " << pFileName << "\n";
32        std::cout << "Make sure the file or directory has read access\n";
33        return false;
34    }
35
36    fseek(fp, 0, SEEK_END);
37    const long fileLength = ftell(fp);
38    fseek(fp, 0, SEEK_SET);
39    if (!fileLength)
40    {
41        std::cout << pFileName << " has zero length\n";
42        fclose(fp);
43        return false;
44    }
45
46    image.resize((size_t)fileLength);
47    fread(&image[0], 1, image.size(), fp);
48    fclose(fp);
49    return true;
50}
codekingpro/portable-devtools · Team Ai