二进制文件读写rb与rb+

Posted on   yacbo  阅读(1801)  评论(0编辑  收藏  举报

二进制的文件读写 rb与 rb+

rb:为输入打开一个二进制文件; 

rb+ 指为读写打开一个二进制文件;

rb为只读,对于不需要进行更新的文件,可以防止用户的错误的写回操作,防止损毁原有数据。具有较高的安全性~~~
rb+为更新二进制文件,可以读取,同时也可以写入,需要用到fseek之类的函数进行配合,以免出错~~~
1
2
3
FILE* fp = nullptr;
const char* fname = "../test.h264";
fp = fopen(fname, "rb+");<br>if(!fp)<br>{<br>  std::cout<<"打开文件失败!"<<endl;<br>}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef COPYDIR_H
#define COPYDIR_H
 
#include <string>
#include <vector>
 
class CopyDir
{
public:
    CopyDir();
    ~CopyDir();
    void copy(const std::string& srcDirPath, const std::string& desDirPath);
 
public:
 
private:
    bool make_dir(const std::string& pathName);
    //bool mkdir (char const* pathname/*, mode_t mode*/);
    bool get_src_files_name(std::vector<std::string>& fileNameList);
    void do_copy(const std::vector<std::string>& fileNameList);
 
private:
    std::string srcDirPath, desDirPath;
};
 
#endif // COPYDIR_H

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "copy_dir.h"
#include <iostream>
#include <fstream>
#include <cstring>
#pragma warning(disable: 4996)
 
#if defined(_WIN32)
#   include <direct.h>
#   include <io.h>
#   include <shlobj.h>
#   include <sys/stat.h>
#   include <sys/types.h>
#else // Linux
#   include <dirent.h>
#   include <unistd.h>
#   include <sys/stat.h>
#   include <sys/types.h>
#   include <pwd.h>
#endif
 
CopyDir::CopyDir()
{
 
}
 
CopyDir::~CopyDir()
{
 
}
 
void CopyDir::copy(const std::string& srcDirPath, const std::string& desDirPath)
{
    this->srcDirPath = srcDirPath;
    std::string srcDir;
#ifdef _WIN32
    int n = 0;
    while (srcDirPath.find('\\', n) != std::string::npos)
    {
        n = srcDirPath.find('\\', n) + 1;
    }
    if (n == 0)
    {
        std::cout << "src path error" << std::endl;
        return;
    }
    srcDir = srcDirPath.substr(n - 1, srcDirPath.size());
 
#else  // Linux
    int n = 0;
    while (srcDirPath.find('/', n) != std::string::npos)
    {
        n = srcDirPath.find('/', n) + 1;
    }
    if (n == 0)
    {
        std::cout << "src path error" << std::endl;
        return;
    }
    srcDir = srcDirPath.substr(n - 1, srcDirPath.size());
 
#endif
    this->desDirPath = desDirPath + srcDir;
 
    if (!make_dir(this->desDirPath))
    {
        return;
    }
 
    std::vector<std::string> fileNameList;
    if (!get_src_files_name(fileNameList))
    {
        return;
    }
 
    if (fileNameList.empty())
    {
        std::cout << "src dir is empty" << std::endl;
        return;
    }
 
    do_copy(fileNameList);
}
 
bool CopyDir::make_dir(const std::string& pathName)
{
    if (access(pathName.c_str(), 0) == -1)  //文件夹不存在则生成一个
    {
        std::cout << pathName << " is not existing" << std::endl;
        std::cout << "now make it" << std::endl;
        #ifdef _WIN32
                if (::_mkdir(pathName.c_str()) < 0)
                {
                    std::cout << "create path error" << std::endl;
                    return false;
                }
        #else  // Linux
                if (::mkdir(pathName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP) < 0)
                {
                    std::cout << "create path error" << std::endl;
                    return false;
                }
        #endif
    }
    return true;   
}
 
bool CopyDir::get_src_files_name(std::vector<std::string>& fileNameList)
{
    #ifdef _WIN32
        _finddata_t file;
        long lf;
        std::string src = this->srcDirPath + "\\*.*";
        if ((lf = _findfirst(src.c_str(), &file)) == -1)
        {
            std::cout << this->srcDirPath << " not found" << std::endl;
            return false;
        }
        else{
            while (_findnext(lf, &file) == 0)
            {
                if (strcmp(file.name, ".") == 0 || strcmp(file.name, "..") == 0)
                    continue;
                fileNameList.push_back(file.name);
            }
        }
        _findclose(lf);
    #else  // Linux
        DIR *dir;
        struct dirent *ptr;
 
        if ((dir = opendir(this->srcDirPath.c_str())) == NULL)
        {
            std::cout << this->srcDirPath << " not found" << std::endl;
            return false;
        }
 
        while ((ptr = readdir(dir)) != NULL)
        {
            if ((ptr->d_name == ".") || (ptr->d_name == ".."))  //current / parent
                continue;
            else if (ptr->d_type == 8)  //file
                fileNameList.push_back(ptr->d_name);
            else if (ptr->d_type == 10)  //link file
                continue;
            else if (ptr->d_type == 4)  //dir
                fileNameList.push_back(ptr->d_name);
        }
        closedir(dir);
 
    #endif
    return true;
}
 
void CopyDir::do_copy(const std::vector<std::string> &fileNameList)
{
#pragma omp parallel for
    for (int i = 0; i < fileNameList.size(); i++)
    {
        std::string nowSrcFilePath, nowDesFilePath;
#ifdef _WIN32
        nowSrcFilePath = this->srcDirPath + "\\" + fileNameList.at(i);
        nowDesFilePath = this->desDirPath + "\\" + fileNameList.at(i);
 
#else
        nowSrcFilePath = this->srcDirPath + "/" + fileNameList.at(i);
        nowDesFilePath = this->desDirPath + "/" + fileNameList.at(i);
 
#endif
        std::ifstream in;
        in.open(nowSrcFilePath, std::ios::binary);
        if (!in)
        {
            std::cout << "open src file : " << nowSrcFilePath << " failed" << std::endl;
            continue;
        }
 
        std::ofstream out;
        out.open(nowDesFilePath, std::ios::binary);
        if (!out)
        {
            std::cout << "create new file : " << nowDesFilePath << " failed" << std::endl;
            in.close();
            continue;
        }
 
        out << in.rdbuf();
 
        out.close();
        in.close();
    }
}

  

  

1
2
3
4
5
6
7
8
9
10
11
#ifdef _WIN32
    std::string src = "D:\\a";
    std::string des = "C:";
    CopyDir cd;
    cd.copy(src, des);
#else
    std::string src = "/media/myUbuntu/F/data/test";
    std::string des = "/media/myUbuntu/F/data/test2";
    CopyDir cd;
    cd.copy(src, des);
#endif

  

随笔 - 186, 文章 - 0, 评论 - 4, 阅读 - 19万

Copyright © 2025 yacbo
Powered by .NET 9.0 on Kubernetes

点击右上角即可分享
微信分享提示