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!";
//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!";
//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!";
Code Example |
---|
Cpp :: mkdir c++ |
Cpp :: fstring from float c++ ue4 |
Cpp :: how to change a value from an array c++ |
Cpp :: c++ template example |
Cpp :: how to pass function as a parameter in c++ |
Cpp :: C++ string initialization |
Cpp :: cpp float to string |
Cpp :: segmented sieve cpp |
Cpp :: how to iterate throguh a string in c++ |
Cpp :: convert string to lpstr |
Cpp :: splice string in c++ |
Cpp :: c++ program transpose of matrix |
Cpp :: get value of enum cpp |
Cpp :: c++ check palindrome |
Cpp :: pop_back |
Cpp :: c++ get string between two characters |
Cpp :: clear qlayout |
Cpp :: convert unsigned long to string c++ |
Cpp :: how to reverse a vector |
Cpp :: filling 2d array with 0 c++ |
Cpp :: c++ casting |
Cpp :: hello world in c/++ |
Cpp :: methods available for a stl vector |
Cpp :: c++ filesystem read directory |
Cpp :: c++ check if string is isogram |
Cpp :: doubly linked list in cpp |
Cpp :: c++ replace |
Cpp :: c++ initialize static variable |
Cpp :: cpp define function |
Cpp :: factorial of large number |