1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | #include "iostream" #include "filesystem" #include "fstream"int count = 0; std::string Message = "" ; //读取文件内容,C风格 int ReadFile( char * FileName, char ** Buf, char * ResponseData) { FILE * fp = fopen (FileName, "rb" ); if (!fp) { sprintf (ResponseData, "{\"data\":{},\"code\":\"1\".\"msg\":\"Missing\"}\r\n" ); printf ( "%s" ,ResponseData); return -1; } fseek (fp, 0, SEEK_END); //将指针移动到文件尾 int FileLen = ftell (fp); //获取文件长度 fseek (fp, 0, SEEK_SET); //将指针移动到文件头 char * jsonstr = new char [FileLen]; if (!jsonstr) { sprintf (ResponseData, "{\"data\":{},\"code\":\"1\",\"msg\":\"Memory Error\"}\r\n" ); printf ( "%s" , ResponseData); fclose (fp); return -2; } memset (jsonstr, 0, FileLen); int rLen = fread (jsonstr, 1, FileLen, fp); if (rLen != FileLen) //rLen为读取到的长度,FileLen为文件长度 { sprintf (ResponseData, "{\"data\":{},\"code\":\"1\",\"msg\":\"Load Json Error\"}\r\n" ); printf ( "%s" , ResponseData); fclose (fp); delete [] jsonstr; return -3; } *Buf = jsonstr; fclose (fp); return 0; } //读取文件内容,C++风格 void ReadFile(std::string FileName, std::string& out_Data, size_t & out_Size) { std::ifstream in_array_file; in_array_file.open(FileName.c_str(), std::ios::in || std::ios::binary); if (in_array_file.is_open()) { unsigned char * buffer_bmp = nullptr ; unsigned int size_tmp = 0; /***********获取文件大小***********/ in_array_file.seekg(0, std::ios::end); size_tmp = in_array_file.tellg(); in_array_file.seekg(0, std::ios::beg); /***********读取文件***********/ buffer_bmp = new unsigned char [size_tmp+1]; if (buffer_bmp != nullptr ) { memset (buffer_bmp, 0, size_tmp + 1); in_array_file.read(( char *)buffer_bmp, size_tmp); } in_array_file.close(); /***********拷贝文件内容***********/ out_Data = ( char *)buffer_bmp; out_Size = out_Data.length(); if (buffer_bmp != nullptr ) { delete [] buffer_bmp; } } } //在指定位置修改文件内容 void ReviseFile(std::string FileName, std::string Data, int Place) { //ofstream在打开文件时默认清空文件所有内容。如果使用ios::app来打开文件,虽然不会清空文件内容,但是每次写操作都追加到文件末尾,即使使用seekp也没用。 //fstream fs("D:/test.txt", ios::binary | ios::out | ios::app);//使用ios::app来打开文件 //fs.seekp(2, ios::beg);//跳转到开头的第二个字节位置进行写入。 //fs.write("!!!", 3);//写入三个!!!,最后发现还是写在结尾,即使seekp也没用 //fs.close(); //将文件打开模式ios::app替换为ios::in,这样可以保证文件内容不会被清空,且文件指针偏移操作有效。 //fstream fs("D:/test.txt", ios::binary | ios::out | ios::in);//使用ios::in来打开文件 //fs.seekp(2, ios::beg);//跳转到开头的第二个字节位置进行写入 //fs.write("!!!", 3);//写入三个!!!,正常写入 //fs.close(); //中间的位置必须使用feekp然后再写才能成功修改。并且seekp之后,如果继续读一些内容,再写也写不进去。必须seekp之后就写,才写的进去。 std::fstream fs(FileName, std::ios::binary | std::ios::out | std::ios::in); fs.seekp(Place, std::ios::beg); //跳转到开头第Palce个字节 fs.write(Data.c_str(), Data.length()); fs.close(); } //修改文件大小 void ReviseFile(std::string FileName, int Size) { //需要c++17标准库 //在100字节处进行截断,即只有100字节大小,多余100则多余部分舍弃,小于100则补0 //filesystem::resize_file("F:\\test.txt", 100); std::filesystem::resize_file(FileName, Size); } //判断文件是否存在 bool IsFileExists(std::string FileName) { //C++17标准 if (std::filesystem::exists(FileName)) { if (std::filesystem::is_directory(FileName)) { printf ( "%s is a directory\n" , FileName.c_str()); } else if (std::filesystem::is_regular_file(FileName)) { printf ( "%s is a file\n" , FileName.c_str()); } else { printf ( "%s exist\n" , FileName.c_str()); } return true ; } else { printf ( "%s no exist\n" , FileName.c_str()); return false ; } } void main() { /***********从文件中读取数据***********/ std::ifstream fin( "D:/test.txt" ); fin >> count; fin.close(); std::cout << count << std::endl; count++; /***********写入数据到文件中***********/ std::ofstream fout( "D:/test.txt" ); fout << count << "," << ++count << "\n +" ; fout.close(); /***********删除文件***********/ remove ( "D:/test.txt" ); std::ifstream _fin( "D:/data.txt" ); int a ,b; char c; /***********读取数据***********/ _fin >> a >> c >> b; std::cout << "a=" << a << std::endl; std::cout << "b=" << b << std::endl; std::cout << "c=" << c << std::endl; //c为, _fin.close(); a++; b++; std::cout << "change a=" << a << std::endl; std::cout << "change b=" << b << std::endl; //std::ofstream _fout("d:/data.txt");//清空文件重新写入 std::ofstream _fout( "d:/data.txt" ,std::ios::out|std::ios::app);; //在文件末尾追加内容 _fout << a << "," << b << "\n" ; _fout << "test" << "," << "\n" ; _fout << "test" << "," << "\n" ; _fout << "test" << "," << "\n" ; _fout.close(); /***********判断文件是否存在***********/ IsFileExists( "D:/data.txt" ); /***********读取文件内容与大小***********/ std::string data = "" ; size_t length = 0; ReadFile( "D:/data.txt" ,data,length); std::cout << "file_data:" << data.c_str() << std::endl; std::cout << "file_length:" << length << std::endl;<br><br> CheckAndCreateFile( "E:/TEST/test.txt" ); } |
#ifdef WIN32 //Windows
#include <direct.h>
#include <io.h>
#else // Linux
#include <sys/io.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#endif
bool CheckAndCreateFile(std::string FileName)
{
FileName = FileName.substr(0, FileName.find_last_of('/'));
std::cout << "filename:" << FileName.c_str() << std::endl;
//判断该文件夹是否存在
if (access(FileName.c_str(), 0) == -1)
{
#ifdef WIN32
int flag = mkdir(FileName.c_str()); //Windows创建文件夹
#else
int flag = mkdir(FileName.c_str(), S_IRWXU); //Linux创建文件夹
#endif
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!