文件管理操作之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;
}

结束啦,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值