linuxc++⽬录操作,C++⽂件及⽂件夹操作整理(代码⽰例)⼀ ⽂件
1.1 使⽤C++标准库中的IO库(fstream)读写⽂件
#include
#include
using namespace std;
int main()
{
char szData[200] = "123456 test";
fstream fFile;
fFile.open("", ios::app | ios::out | ios::in);
/****************将数据写⼊⽂件-begin***************/
fFile << szData;
/****************将数据写⼊⽂件-end***************/
/*************** 将数据从⽂件中读取出来-begin******************/
fFile.seekg(0, ios::end);
int iSize = llg(); //计算出⽂件⼤⼩
fFile.seekg(ios::beg); //从⽂件最前⾯开始读取
fFile >> noskipws; //设置读取空格、回车
std::string strDataOut;
for (int i = 0; i < iSize/*!f()*/; i++)
{
char c;
fFile >> c;
strDataOut.push_back(c);
}
cout << strDataOut.c_str();
/*************** 将数据从⽂件中读取出来-end******************/
fFile.close();
return 0;
}
1.2 使⽤windows API读写⽂件
#include
#include
int main()
{
std::string strFileName = "";
/*************************写⽂件-begin******************************/
std::string strData = "123456 test";
DWORD dwReturn;
HANDLE hFileWrite = CreateFileA(strFileName.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE != hFileWrite)
{
WriteFile(hFileWrite, strData.c_str(), strData.length(), &dwReturn, NULL);
CloseHandle(hFileWrite);
}
/*************************写⽂件-end******************************/
/*************************读⽂件-begin******************************/
DWORD bytesRead = 0;
char szBuffer[1024] = { 0 };
HANDLE hFileRead = CreateFileA(strFileName.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE != hFileRead)
{
ReadFile(hFileRead, szBuffer, 1024/*static_cast(length)*/, &bytesRead, NULL);
CloseHandle(hFileRead);
}
/*************************读⽂件-end******************************/
return 0;
}
1.3 linux读写⽂件
#include
#include
#include
#include
int main()
{
std::string strPath = "";
/
*************************写⽂件-begin******************************/
int iFileWrite = ::open(strPath.c_str(), O_TRUNC | O_APPEND | O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if ( -1 == iFileWrite)
{
return 0;
}
std::string strBuffer = "Test Data";
int n = write(iFileWrite, strBuffer.c_str(), strBuffer.length());
::close(iFileWrite);
/*************************写⽂件-end******************************/
/*************************读⽂件-begin******************************/
char szBuffer[1024] = { 0 };
int iFileRead = ::open(strPath.c_str(), O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (-1 == iFileRead)
{
return 0;
}
read(iFileRead, szBuffer, 1024);
std::cout << szBuffer;
::close(iFileRead);
/*************************读⽂件-end******************************/
return 0;
}
⼆ ⽂件夹
1.1 Windows
1. 创建⽂件夹
#include
#include
#include
using namespace std;
int main()
{
string folderPath = "E:\Test\Dir";
if (0 != access(folderPath.c_str(), 0))
{
int iRst = mkdir(folderPath.c_str()); // 需要迭代创建,即创建⼦⽂件夹时⽗⽂件夹必须存在
}
return 0;
}
2. 遍历⽂件夹
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
using namespace std;
//获取⽂件夹下所有⽂件名及⽂件夹总⼤⼩
DWORD TraversalFolder(string strPath, vector& files)
{
DWORD dwRtn = 0;
long hFolder = 0; //⽂件句柄
struct _finddata_t fileinfo; //⽂件信息
string strFileName = "";
if ((hFolder = _findfirst(strFileName.assign(strPath).append("\*").c_str(), &fileinfo)) != -1)
{
do
{
DWORD dwSize = 0;
//如果是⽬录,迭代之;如果不是,加⼊列表
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
dwSize = TraversalFolder(strFileName.assign(strPath).append("\").append(fileinfo.name), files); }
}
else
{
files.push_back(strFileName.assign(strPath).append("\").append(fileinfo.name));
dwSize = fileinfo.size;
}
dwRtn += dwSize;
} while (0 == _findnext(hFolder, &fileinfo));
_findclose(hFolder);
}
return dwRtn;
}
int main()
{
char * filePath = "E:/test";
DWORD dwFolderSize;
vector files;
dwFolderSize = TraversalFolder(filePath, files);//获取⽂件夹下所有⽂件名及⽂件夹总⼤⼩
system("pause");
}
1.2 Linux
1. 创建⽂件夹
#include
#include
#include
int main()
{
std::string strParh = "Test111";
int isCreate = ::mkdir(strParh.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);// // 需要迭代创建,即创建⼦⽂件夹时⽗⽂件夹必须存在
if (0 == isCreate)
{
std::cout << "mkdir succeeded";linux怎么读取windows文件
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论