文件管理操作之c++版本 cpp和流操作
2021/6/17 22:28:28
本文主要是介绍文件管理操作之c++版本 cpp和流操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
首先来看一下c语言的操作
//c语言的写入 void main() { int ar[] = { 10, 11, 22, 33, 44, 55, 66, 77, 88, 99 }; int size = sizeof(ar) / sizeof(ar[0]); FILE* fp = fopen("Test1.txt", "w"); assert(fp!=NULL); // "w" 是写入,创建一个fp的txt 功能是写入 for (int i = 0; i < size; ++i) { fprintf(fp, "%d ", ar[i]); //fprintf 在fp中以整形输入ar[i] } fclose(fp); } void main() { string str; while (getline(cin, str)) { cout << str<< endl; } }
//c语言的读 void main() { int ar[10]; FILE *fp = fopen("Test1.txt", "r"); assert(fp != NULL); for (int i = 0; i<10; ++i) fscanf(fp, "%d", &ar[i]); fclose(fp); }
c++中与c不同的是基于对象,用函数操作。下面是c++版本
通过文本文件读写 都是字符
cpp的输入流写法
void main() { int ar[] = { 11, 22, 33, 44, 55, 66, 77, 88, 99 }; int n = sizeof(ar) / sizeof(ar[0]); ofstream ofile; ofile.open("Test2.txt", ios::out); assert(ofile); for (int i = 0; i < n; ++i) ofile << ar[i] << " "; ofile << endl; ofile << "end"; ofile.close(); }
void main() { int ar[10]; ifstream ifile; ifile.open("Test2.txt", ios::in); assert(ifile); for (int i = 0; i < 10; ++i) ifile >> ar[i]; ifile.close(); }
接下来看看c语言和c++的二进制读写的操作
因为代码段太多了 我就全部写一个编码段了,没有分开
每部是怎样操作干什么已经在代码段里写清楚啦!
//c++的读写 void main() { int ar[10]; int size = sizeof(ar) / sizeof(ar[0]); ifstream ifile; ifile.open("Test22.txt", ios::in | ios::binary); //二进制写入 因为二进制没有转化 所以是乱码 assert(ifile); ifile.read((char*)ar, sizeof(int)*size); ifile.close(); } //c++的写文件 void main() { int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; int size = sizeof(ar) / sizeof(ar[0]); ofstream ofile; ofile.open("Test22.txt", ios::out | ios::binary); //二进制写入 因为二进制没有转化 所以是乱码 assert(ofile); ofile.write((const char*)ar, sizeof(int)*size); ofile.close(); } //c语言的二进制读取 void main() { int ar[10]; int size = sizeof(ar) / sizeof(ar[0]); FILE* fp = fopen("Test11.txt", "rb"); //二进制读出 assert(fp != NULL); fread(ar,sizeof(int),10,fp); fclose(fp); } void main() { int ar[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; int size = sizeof(ar) / sizeof(ar[0]); FILE* fp = fopen("Test11.txt", "wb"); //二进制写入 因为二进制没有转化 所以是乱码 assert(fp != NULL); fwrite(ar, size, sizeof(int), fp); fclose(fp); }
配置管理器 就是写一个文本 然后通过类来才做读写,代码如下,用ip和端口做例子。
//配置管理类的作用是将ip和端口的内容,从电脑中读取出来放在一个结构体里面 struct ServerInfo { char _ip[32]; int _port; }; class ConfigManager { public: ConfigManager(string cfg = "ghcserver.txt") :_configfile(cfg) {} public: void ReadInfo(ServerInfo &info)//把结构体放进去用来存数据 { ifstream ifile(_configfile.c_str(), ios::in);//.c_str() string对象转化为指针类型 assert(ifile); ifile >> info._ip >> info._port; ifile.close(); } void WriteInfo(ServerInfo &info) { ofstream ofile(_configfile.c_str(), ios::out); assert(ofile); ofile << info._ip << " " << info._port; ofile.close(); } private: string _configfile; };
将整型数字改成字符串有两种做法,一种是利用函数,一种是以流作为中间商,转化。代码如下:
//做法2 void main() { int a = 783952; string str; stringstream ss; //好处是 不用考虑空间 //先将a输出给字符串流ss ss << a; //再将处理好的ss 输入给str; ss >> str; cout << str << endl; //ss底层就有一个str对象 cout << ss.str() << endl; //ss使用后 必须清除一下 否则原来的数据依旧存在 //因为以 0/ 结尾的 所以下一个进来 也看不到 ss.clear(); int b = 8384; ss << b; ss >> str; cout << str << endl; } //做法1 //整形转化字符串的情况 void main() { int a = 986725; char str[30] = { 0 }; //将a中的每一个末尾 处理成一个字符存入str中 //参数是 数字 存入的位置 以几进制处理 //itoa(a, str, 10); sprintf(str, "%d", a); //给str中以%d的形式放a cout << str << endl; }
结束啦,谢谢
这篇关于文件管理操作之c++版本 cpp和流操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-28pyqt 怎么打包整个项目-icode9专业技术文章分享
- 2024-09-28laravel Commands 创建带有参数的 Artisan 命令的步骤和示例-icode9专业技术文章分享
- 2024-09-28antd怎么实现渲染tiff图片-icode9专业技术文章分享
- 2024-09-28英文半角中划线和中文全角的中划线有什么区别-icode9专业技术文章分享
- 2024-09-28nvm npm 和node 他们之间有什么关系-icode9专业技术文章分享
- 2024-09-28Node Version Manager (nvm)使用教程-icode9专业技术文章分享
- 2024-09-28nvm命令太慢,是什么原因-icode9专业技术文章分享
- 2024-09-28Kotlin 如何增加、删除和修改 MutableStateFlow 中的值。-icode9专业技术文章分享
- 2024-09-28Kotlin的stateFlow.update 写法介绍-icode9专业技术文章分享
- 2024-09-28kotlin 怎么获取当前时间格式-icode9专业技术文章分享