C++方法记录1

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值