Search
 
SCRIPT & CODE EXAMPLE
 

CPP

read string from binary file in c++

fstream readStringBF;
	readStringBF.open("Name.dat", ios::in | ios::binary);
	char n[30];
	if (readStringBF.is_open()) {
		readStringBF.read(n, 30);
		n[readStringBF.gcount()] = NULL;		//append string terminator
		cout << n;
		readStringBF.close();
	}
	else cout << "Unable to open file!";
Comment

binary file in c++

				//reading from a binary file
	int r;
	fstream readBinaryFile;
	readBinaryFile.open("Binary file.dat", ios::in | ios::binary);
	if (readBinaryFile.is_open()) {
		readBinaryFile.read((char*)&r, sizeof(r));
		cout << r;
		readBinaryFile.close();
	}
	else cout << "Unable to open file!";
Comment

binary file in c++

    //writing into a binary file
		            //int
	fstream bFile("Binary file.dat", ios::out | ios::binary);
	int x = 65;
	if (bFile.is_open()) {
		bFile.write((char*)&x, sizeof(x));
		bFile.close();
	}
	else cout << "Failed opening the file";

			       	//string
fstream binaryName;
	string s = "J.L";
	binaryName.open("Name.dat", ios::out | ios::binary);
	if (binaryName.is_open()) {
		binaryName.write(s.c_str(), s.size());
		binaryName.close();
	}
	else cout << "Unable to open the file!";
Comment

PREVIOUS NEXT
Code Example
Cpp :: finding no of unique characters in a string c++ 
Cpp :: prime number program 
Cpp :: PI IN C++ WITH CMATH 
Cpp :: finding size of columns and rows in 2d vector c++ 
Cpp :: how to make a 2d vector in c++ 
Cpp :: go through std vector 
Cpp :: C++ passing function arguments to a thread 
Cpp :: c++ triangle 
Cpp :: function as argument in another function in c++ 
Cpp :: how to sort a vector in descending order in c++ 
Cpp :: how to initialize 2d vector in c++ 
Cpp :: how to change string to lowercase and uperCase in c++ 
Cpp :: string to integer convert c++ 
Cpp :: integer type validation c++ 
Cpp :: cpp mst 
Cpp :: c++ ros publisher 
Cpp :: c++ switch string 
Cpp :: string reversal 
Cpp :: allow cross origin 
Cpp :: c++ check if char is number 
Cpp :: sin in c++ 
Cpp :: c++ return multiple values 
Cpp :: max of a vector c++ 
Cpp :: c++ call method in same class 
Cpp :: segmented sieve of Eratosthenes in cpp 
Cpp :: Story of c++ 
Cpp :: find max element in array c++ 
Cpp :: Xor implementation C++ 
Cpp :: how to make an overloaded constructor in c++ 
Cpp :: 2d array c++ 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =