C++文件操作

    xiaoxiao2021-03-26  21

    文本文件

    #include <iostream>

    #include <fstream>

     

    using namespace std;

     

    void main()

    {

        ifstream fin("1.txt", ios::_Nocreate);

        if (!fin)

        {

             cout << "File open error!\n";

             return;

        }

     

        /*****************************************

        // 一个一个字符的读取

        char c;

     

        while ((c = fin.get()) != EOF)

        {

             cout << c;

        }

     

        // 多个字符读取方式1

        char buf[256];

        while (fin.get(buf, 256, '\0'))

        {

             cout << buf;

        }

     

        // 方式2

        while (!fin.eof())

        {

             fin.getline(buf, 256);

             cout << buf;

        }

        //比较:

        //get() 会把\n留在输入队列中,解决方法是截取\n,每次使用get()再次使用get()即可:fin.get(buf,256).get();

        //getline 每次读取一行且把\n抛弃。

     

     

        // 方式3

        char buf[256];

        while (!fin.eof())

        {

             fin.read(buf, 80);

             cout.write(buf, fin.gcount());

        }

     

        *****************************************/

        fin.close();

    }

     

    二进制文件

    #include <iostream>

    #include <fstream>

    #include <ctime>

    using namespace std;

    int main()

    {

        char ch;

        // 输入流

        ifstream in;

        in.open("BigObject.iso", ios::binary);

        if (!in)

        {

             cout << "打开文件失败." << endl;

             return 1;

        }

     

        // 输出流

        ofstream out;

        out.open("BigObject2.iso", ios::binary);

        if (!out)

        {

             cout << "打开文件失败." << endl;

             return 1;

        }

        // 复制数据

        time_t  tmBegin = time(NULL);

        cout << "正在复制数据..." << tmBegin << endl;

        while (in)

        {

             in.get(ch);

             if (in) out.put(ch);

        }

        time_t  tmEnd = time(NULL);

        cout << "复制数据完成..." << tmEnd << endl;

        in.close();

        out.close();

     

        system("pause");

        return 0;

    }

    注意:这只适合小文件复制,对于大文件(测试文件为3.18G)复制效率不高。(好久......)

    改进:(不到一分钟)

        // 复制数据

        char buf[1024];

     

        time_t  tmBegin = time(NULL);

     

        cout << "正在复制数据..." << tmBegin << endl;

        while (in)

        {

             in.read(buf, 1024);

             out.write(buf, in.gcount());

        }

     

        time_t  tmEnd = time(NULL);

        cout << "复制数据完成..." << tmEnd << endl;

     

    创建文件夹

    #include <string.h>

    #include <direct.h>

    string path = "File://F:\Log";

     

        // 读取盘符

        int nLoc = path.find(":", 0);

     

     

        // F:

        string disk = path.substr(nLoc + 3,2);

        string root = path.substr(nLoc + 5);

        // 读取根目录

        string strFilePath = path.substr(nLoc + 3, 2) + "\\" + path.substr(nLoc + 5);

        // 创建目录

        //_mkdir(strFilePath.c_str());

        strFilePath += "\\192.168.1.100";

        strFilePath += "\\2016-12-21";

        strFilePath += "\\00H";

        strFilePath += "\\APP1";

     

        // 创建目录

        char* cmd = new char[strlen(strFilePath.c_str()) + 3];

        sprintf(cmd, "md %s", strFilePath.c_str());

        // 目录不存在则创建

        if (0!=_access(strFilePath.c_str(),0))

        {

             system(cmd);

        }

     

     

     

     

    转载请注明原文地址: https://ju.6miu.com/read-350338.html

    最新回复(0)