Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I write this program for learning write and read string in binary file.

C++
// testtest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main( )
{
	string str1 = "abababab";
	string str2 = "wjwjwjwjwjwj";
	string str3 = "vivivivivivivivi";

	int size1 = (str1.size());
	int size2 = (str2.size());
	int size3 = (str3.size());

	ofstream output ("output.txt", ios::out | ios::binary);
	if (!output)
	{
		cout << "can't open file" << endl;
		exit(1);
	}

	output.write(reinterpret_cast<char *>(&size1), sizeof(int));
	output.write(str1.c_str(), size1);

	output.write(reinterpret_cast<char *>(&size2), sizeof(int));
	output.write(str2.c_str(), size2);

	output.write(reinterpret_cast<char *>(&size3), sizeof(int));
	output.write(str3.c_str(), size3);

	str1 = str2 = str3 = "";
	size1 = size2 = size3 = 0;

	output.flush();
	output.close();
	//=======================================================================
	ifstream input("output.txt", ios::in | ios::binary);
	if (!output)
	{
		cout << "can't open file" << endl;
		exit(1);
	}

	input.read(reinterpret_cast<char *>(&size1), sizeof(int));
	input.read(reinterpret_cast<char *>(&str1), size1);

	input.read(reinterpret_cast<char *>(&size2), sizeof(int));
	input.read(reinterpret_cast<char *>(&str2), size2);

	input.read(reinterpret_cast<char *>(&size3), sizeof(int));
	input.read(reinterpret_cast<char *>(&str3), size3);

	cout << str1 << endl << str2 << endl << str3 << endl << endl;

	return 0;
}


program read str1 and str2 correctly but can't read str3 . visual c++ return these messages when I debug program:

Unhandled exception at 0x51EDA9E8 (msvcr120d.dll) in testtest.exe: 0xC0000005: Access violation reading location 0x6976696A.

and

str3: Error reading characters of string
Posted
Updated 19-Mar-15 21:36pm
v3
Comments
Timberbird 20-Mar-15 5:23am    
Your code seems a bit strange - do you really intend to read std::string object in file, not the string (char*) itself? When you read like
input.read(reinterpret_cast<char *="">(&str3), size3);
, it's not like "take size3 bytes from file and place them into string str3", it's "read size3 bytes from file and place them into memory, starting with address &str3". You may occasionally corrupt memory state this way. I suggest you try to read into buffer variable (char*) and them assign its value to std::string objects.

1 solution

I solve it

C++
// testtest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main( )
{
	char *buf;
	string str1 = "abababab";
	string str2 = "wjwjwjwjwjwj";
	string str3 = "vivivivivivivivi";

	int size1 = (str1.size());
	int size2 = (str2.size());
	int size3 = (str3.size());

	ofstream output ("output.txt", ios::out | ios::binary);
	if (!output)
	{
		cout << "can't open file" << endl;
		exit(1);
	}

	output.write(reinterpret_cast<char *>(&size1), sizeof(int));
	output.write(str1.c_str(), size1);

	output.write(reinterpret_cast<char *>(&size2), sizeof(int));
	output.write(str2.c_str(), size2);

	output.write(reinterpret_cast<char *>(&size3), sizeof(int));
	output.write(str3.c_str(), size3);

	str1 = str2 = str3 = "";
	size1 = size2 = size3 = 0;

	output.flush();
	output.close();
	//=======================================================================
	ifstream input("output.txt", ios::in | ios::binary);
	if (!output)
	{
		cout << "can't open file" << endl;
		exit(1);
	}

	input.read(reinterpret_cast<char *>(&size1), sizeof(int));
	buf = new char[size1];
	input.read( buf, size1);
	str1 = "";
	str1.append(buf, size1);

	input.read(reinterpret_cast<char *>(&size2), sizeof(int));
	buf = new char[size2];
	input.read(buf, size2);
	str2 = "";
	str2.append(buf, size2);

	input.read(reinterpret_cast<char *>(&size3), sizeof(int));
	buf = new char[size3];
	input.read(buf, size3);
	str3 = "";
	str3.append(buf, size3);

	cout << str1 << endl << str2 << endl << str3 << endl << endl;

	return 0;
}
 
Share this answer
 
v2
Comments
Timberbird 20-Mar-15 5:28am    
Oh, I see I was a bit late :). Yes, this solution with buffer variable should work. I can only advice that you also release memory allocated for buf variable by calling delete. Also, you don't need
strN = "";
assignments when reading. And, finally, instead of string.append() (which appends a string to already contained value) you can use string.assign().

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900