C++方法记录1

来源:互联网 发布:linux ls 查找文件名 编辑:程序博客网 时间:2024/05/21 17:37

1、判断文件夹是否存在,若不存在则创建

#include<direct.h>bool isExist=mkdir("./save1");//只可建立一级目录 "./"表示当前路径,有无皆可;"../"表示父路径

返回-1时,代表建立文件夹不成功,即文件夹存在或输入路径为多级未建立路径;返回0时,建立文件夹成功

其他文件夹操作:http://blog.csdn.net/woodsp/article/details/54923937

当前路径相关:http://www.cnblogs.com/cgli/archive/2012/12/03/2800468.html

http://blog.csdn.net/lincyang/article/details/6240008

http://blog.csdn.net/zhongguoren666/article/details/7731772

http://www.cnblogs.com/codingmengmeng/p/6257722.html

2、创建文件

#include<fstream>ofstream ofs;ofs.open("./save1/test1.txt");ofs << "test1" << endl;ofs.close();

3、用系统时间作为新建文件名称(获取并格式化当前系统时间)

#include<time.h>        
time_t rawtime;struct tm *info;char fileName[80];time(&rawtime);info = localtime(&rawtime);strftime(fileName, 80, "%Y%m%d%H%M%S", info);string file = fileName;
参考:http://www.cnblogs.com/Asa-Zhu/archive/2012/08/30/2664341.html

    http://blog.csdn.net/jenny_84/article/details/44804141


4、从文件中读取全部内容

#include <string>  #include <fstream>  #include <sstream>  std::ifstream t("file.txt");  std::stringstream buffer;  buffer << t.rdbuf();  std::string contents(buffer.str());


0 0
原创粉丝点击