c++ 指针读写文件操作相关

二进制指针读写

#include <iostream>
#include <fstream>
void write_out(void *p, long int bitsize,string path)
{
      ofstream  out;
      out.open(path, std::ofstream::binary);  //"/root/res.bin"
      out.write(reinterpret_cast<const char*>(p), bitsize);
      out.close();
}

void *read_in(long int bitsize,string path)
{
      char *p = new char[bitsize];
      ifstream  in;
      in.open(path, std::ofstream::binary);
      in.read(reinterpret_cast<const char*>(p), bitsize);
      in.close();
      return (void*)p;
}

void *read_all(string path)
{
      
      ifstream  in;
      in.open(path, std::ios::in | std::ios::binary);

      in.seekg(0, std::ios::end);
      long int bitsize = in.tellg();
      in.seekg(0, std::ios::beg);

      char *p = new char[bitsize];
      in.read(reinterpret_cast<const char*>(p), bitsize);
      in.close();
      return (void*)p;
}

iostream关联读写

int main()
{
    string in_path = "/c/in", out_path = "/c/out";
    // 读
    freopen(in_path, "r", stdin);
    // 写
    freopen(out_path, "w", stdout);
}

读入优化

  • cin,cout
io::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
  • 快读与快写(整数)
int read() {
    int x=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
    while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}

void write(int x) {
     if(x<0) putchar('-'),x=-x;
     if(x>9) write(x/10);
     putchar(x%10+'0');
}

Linux目录相关

  • 当前目录
man 2 getcwd

char *get_current_dir_name();

  • 流复制文件
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
void copy(const char *ep, const char *cp){
    FILE *in, *out; char buff[1024]; int len;
    in = fopen(ep, "r+");
    out = fopen(cp, "w+");
    while(len = fread(buff, 1, sizeof(buff), in))
        fwrite(buff, 1, len, out);
}
  • 删除文件
man 3 remove

int remove(const char *name);
  • 输出当前时间
time_t t = time(NULL);
char ch[64] = { 0 };
strftime(ch, sizeof(ch) - 1, "D:/pic/M31_/%Y-%m-%d-%H-%M-%S.fit", localtime(&t));     //年-月-日 时-分-秒
  • 计时 微秒级
#include <chrono>
std::chrono::system_clock::time_point start = std::chrono::system_clock::now()
//code

std::chrono::system_clock::time_point end = std::chrono::system_clock::now();

std::cout << "花费了" << std::chrono::duration_cast<std::chrono::microseconds> 
(end - start).count() << "微秒\n";

  • 读取目录下文件
点击查看代码

#ifdef linux
#include <memory.h>
#include <dirent.h>
std::vector<string> getFilesList(string dirpath) {
	vector<string> allPath;
	DIR *dir = opendir(dirpath.c_str());
	if (dir == NULL)
	{
		cout << "opendir error" << endl;
		return allPath;
	}
	struct dirent *entry;
	while ((entry = readdir(dir)) != NULL)
	{
		if (entry->d_type == DT_DIR) {//It's dir
			if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
				continue;
			string dirNew = dirpath + "/" + entry->d_name;
			vector<string> tempPath = getFilesList(dirNew);
			allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());

		}
		else {
			//cout << "name = " << entry->d_name << ", len = " << entry->d_reclen << ", entry->d_type = " << (int)entry->d_type << endl;
			string name = entry->d_name;
			if (name.find(".fit") != std::string::npos) {
				string imgdir = dirpath + "/" + name;
				//sprintf("%s",imgdir.c_str());
				allPath.push_back(imgdir);
			}
		}

	}
	closedir(dir);
	//system("pause");
	return allPath;
}
#endif

#ifdef _WIN32//__WINDOWS_
#include <io.h>  
vector<string> getFilesList(string dir)
{
	vector<string> allPath;
	// 在目录后面加上"\\*.*"进行第一次搜索
	string dir2 = dir + "\\*.*";

	intptr_t handle;
	_finddata_t findData;

	handle = _findfirst(dir2.c_str(), &findData);
	if (handle == -1) {// 检查是否成功
		cout << "can not found the file ... " << endl;
		return allPath;
	}
	while (_findnext(handle, &findData) == 0)
	{
		if (findData.attrib & _A_SUBDIR)//// 是否含有子目录
		{
			//若该子目录为"."或"..",则进行下一次循环,否则输出子目录名,并进入下一次搜索
			if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
				continue;
			// 在目录后面加上"\\"和搜索到的目录名进行下一次搜索
			string dirNew = dir + "\\" + findData.name;
			vector<string> tempPath = getFilesList(dirNew);
			allPath.insert(allPath.end(), tempPath.begin(), tempPath.end());
		}
		else //不是子目录,即是文件,则输出文件名和文件的大小
		{
			string filePath = dir + "\\" + findData.name;
			if (filePath.find(".fit") != std::string::npos) {
				allPath.push_back(filePath);
			}
		}
	}
	_findclose(handle);    // 关闭搜索句柄
	return allPath;
}
#endif
posted @ 2022-08-10 16:01  InsiApple  阅读(159)  评论(0编辑  收藏  举报