C++ IO相关代码段

1、判断文件目录是否存在,不存在则创建:

#include <io.h>
#include <direct.h>


char dir[]=".\\data";
if(_access(dir,NULL))
{
   _mkdir(dir);  
}

2、保存内容到文件中(相对路径,绝对路径,文件名)

 1 #include <iostream.h>
 2 
 3 //文件名
 4 std::string filename="test.txt";
 5 //相对路径
 6 std::string filename_1=".\\data\\test.txt";
 7 //绝对路径
 8 std::string filename_2="c:\\data\\test.txt";
 9 
10 std::ofstream file(filename);
11 
12 
13 if(!file)
14 {
15   std::cout<<"file is not found"<<std::endl;  
16 }
17 
18 file<<"ssdasdasd"<<endl;
19 file.close();

 

posted @ 2021-04-02 15:06  树下看猴  阅读(103)  评论(0编辑  收藏  举报