#include <iostream>
#include <fstream>
#include <cstring>
struct Data {
char *Hold1;
char *Hold2;
};
int main() {
//Variables
int choice , loop = 0, RecAmount = 0;
std::string DepName, output1;
double inp2, output2;
////File Check
std::fstream inventory;
inventory.open("inventory.dat", std::ios::out|std::ios::in|std::ios::binary );
if (inventory.fail()){
inventory.open("inventory.dat", std::ios::out|std::ios::in | std::ios::binary|
std::ios::trunc);
std::cout << "Error opening file....";
return 0; }
while (loop < 1) {
std::cout << "\nRead/write testing\n";
std::cout << "1. Input Data\n";
std::cout << "2. Read Data Output\n";
std::cin >> choice;
////Input 2 strings
if (choice == 1) {
std::cout << "Testing input\n";
struct Data test1;
test1.Hold1 = "First test";
test1.Hold2 = 29;
}
if (choice == 2) {
std::cout << "Testing output\n";
//// Add way to choose which data is read and displayed
inventory.read ((char *)& inp2, sizeof(Data));
std::cout << "Character input = " << output1 << std::endl;
}
}
}
I don't know how to access or use Binary files. I need to:
- Write numbers and character arrays to a binary file (named "inventory.dat" in this)
- Be able to seek and read back chosen information (say I input 3 sets of data and want to only display the 2nd set)
- Overwrite any chosen data (find the second set of data and change it)
I've tried looking up info before and couldn't find anything I could understand or use so any help would be greatly appreciated.