C++读写文本数据

   1、C++读写二进制文件

        /*****************************************************************
	* \author DreamerZhang
	* \date 2019.08.27
	* \brief 读取二进制文件,数据类型为short
	* \param
			fileName-文件名
			readData-读取的数据
			dataLength-返回的数据长度
	* \return true-成功 false - 失败
	*
	*****************************************************************/
	bool readFromFile(std::string fileName, short* readData, int* dataLength);


	/*****************************************************************
	* \author DreamerZhang
	* \date 2019.08.27
	* \brief 保存二进制文件,数据类型为short
	* \param
	fileName-文件名
	data-写入的数据
	dataLength-写入的数据长度
	* \return true-成功 false - 失败
	*
	*****************************************************************/
	bool writeToFile(std::string fileName, short* data, int dataLength);

bool FileTest::readFromFile(std::string fileName, short* readData, int* dataLength)
{
	//seekg()是对输入流的操作 g是get缩写; seekp()是对输出流的操作 p是put缩写
	//它有三个取值: ios::beg:表示输入流的开始位置
	//				ios::cur:表示输入流的当前位置
	//				ios::end:表示输入流的结束位置

	if (fileName.empty()) {
		return false;
	}
	ifstream inFile;
	//打开文件
	inFile.open(fileName, ios::binary);
	if (!inFile.is_open()) {
		std::cout << "Open File Failed.\n" << endl;
		return false;
	}


	//获取文件长度
	inFile.seekg(0, ios::end);
	int bytes = inFile.tellg();
	int length = bytes / sizeof(short);
	inFile.seekg(0, ios::beg);
	//读取文件
	inFile.read((char*)readData, sizeof(short) * length);//注意指针转为char*
	inFile.close();
	*dataLength = length;
	return true;
}

bool FileTest::writeToFile(std::string fileName, short* data, int dataLength)
{
	if (fileName.empty()) {
		return false;
	}
	ofstream outFile;

	//打开文件
	outFile.open(fileName, ios::binary);
	if (!outFile.is_open()) {
		std::cout << "Open File Failed.\n" << endl;
		return false;
	}
	//写入数据
	outFile.write((char*)data, dataLength * sizeof(short));
	outFile.close();

	return true;
}

2、C++读写ASCII文件(cin & cout)

       /*****************************************************************
	* \author DreamerZhang
	* \date 2019.10.22
	* \brief 读取txt文件(ascii码),数据类型为short
	* \param
		fileName-文件名
		readData-读取的数据
		dataLength-返回的数据长度
	* \return true-成功 false - 失败
	*
	*****************************************************************/
	bool readFromFile_Ascii(std::string fileName, short* readData, int* dataLength);

	/*****************************************************************
	* \author DreamerZhang
	* \date 2019.10.22
	* \brief 保存txt文件(ascii码),数据类型为short
	* \param
		fileName-文件名
		data-写入的数据
		dataLength-写入的数据长度
	* \return true-成功 false - 失败
	*
	*****************************************************************/
	bool writeToFile_Ascii(std::string fileName, short* data, int dataLength);

bool FileTest::readFromFile_Ascii(std::string fileName, short* readData, int* dataLength)
{
	if (fileName.empty()) {
		return false;
	}
	ifstream inFile;
	//打开文件
	inFile.open(fileName, ios::in);
	if (!inFile.is_open()) {
		std::cout << "Open File Failed.\n" << endl;
		return false;
	}


	//获取文件长度
	inFile.seekg(0, ios::end);
	int bytes = inFile.tellg();
	int length = bytes / sizeof(short);
	inFile.seekg(0, ios::beg);
	*dataLength = length;

	//读入文件
	for (int i = 0; i < length; i++) {
		inFile >> readData[i];
	}
	
	inFile.close();
	return true;
}

bool FileTest::writeToFile_Ascii(std::string fileName, short* data, int dataLength)
{
	if (fileName.empty()) {
		return false;
	}
	ofstream outFile;
	outFile.open(fileName, ios::out);
	if (!outFile.is_open()) {
		std::cout << "Open File Failed.\n" << endl;
		return false;
	}

	for (int i = 0; i < dataLength; i++)
	{
		outFile << data[i]<<endl;//,流插入,需要换行或者空格
	}
	outFile.close();
}

 

3、主要是利用C函数fread、fwrite、fscanf以及C++文件流ifstream、ofstream等函数从文件读写(以前测试写着玩儿的)。

 

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <String>
using namespace std;
struct student
{
	int num;
	int age;
	char name[30];
	//使用char *name;读取结构体出错
};
//写入结构体数组
void writeStruct()
{
    student* listStruct;
	const int Size = 5;
	listStruct = (struct student*)malloc(sizeof(struct student) * Size);
	FILE *fp;
	for (int i = 0; i < Size; i++){
		listStruct[i].num = i;
		listStruct[i].age = i * 10;
		strcpy(listStruct[i].name, "name");
	}
	fp = fopen("test.txt", "wb");
	if (fp == NULL){
		printf("Open File failed.\n");
		exit(0);
	}
	for (int i = 0; i < Size; i++){
		if (fwrite(&listStruct[i], sizeof(struct student), 1, fp) != 1){
			printf("File Write Error.\n");
		}
	}
	printf("write data success.\n");
	fclose(fp);
}

//读取到结构体
void readStruct(){
	FILE * fp;
	if ((fp = fopen("test.txt", "rb")) == NULL){
		printf("Open File failed.\n");
		exit(0);
	}
	student one;
	printf("read data:\n");
	while (fread(&one, sizeof(struct student), 1, fp) == 1){
		printf("%d %d %s\n", one.num, one.age, one.name);
	}
	fclose(fp);
}


//按照指定格式从文本中读取多行数据
void readFile(){
	FILE *fp;
	fp = fopen("read.txt", "r");
	if (fp == NULL){
		printf("Open File failed.\n");
		exit(0);
	}
	int a;
	float c;
	char str[100];
	while (!feof(fp)){
		fscanf(fp, "%d%s%f", &a, &str, &c);
		printf("Line:%d %s %.1f\n", a, str, c);
	}
	fclose(fp);
}

//C++ 文件流读写
void writeStream(){
	ofstream out;
	out.open("11.txt", ios::trunc);
	if (!out.is_open()){
		cout << "open File Failed." << endl;
		return;
	}
	char a = 'a';
	for (int i = 0; i < 5; i++){
		out << i << "\t" << a << endl;
		a++;
	}
	out.close();
}

void readStream(){
	ifstream in;
	in.open("11.txt", ios::in);
	if (!in.is_open()){
		cout << "open File Failed." << endl;
		return;
	}
	string strOne;
	while (getline(in, strOne)){
		stringstream ss;
		ss << strOne;
		int a;
		char c;
		ss >> a >> c;
		cout << a <<"  "<< c << endl;
	}

}


int main(){
	writeStruct();
	readStruct();
	//readFile();
	//writeStream();
	//readStream();
	return 0;
}


1、测试读写结构体数组

 

2、按照指定格式从文本读取数据

3、C++文件流读写

  • 17
    点赞
  • 164
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值