I have got to create a simple bank account for an employee to manage a customers account, it should have hard coded the customers name, account number and password using declarations but I can't get it to read from the file, any help guys? Thanks guys!
Seeing as you want to read data in binary mode, here you go:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <fstream>
#include <iostream>
int main()
{
char where[100];
std::ifstream input;
where[99] = 0;
input.open("Bankaccount.txt", std::ios_base::binary);
if (input.fail())
std::cout << "Failed to open file" << std::endl;
else
{
input.read(where, sizeof(where) - 1);
input.close();
std::cout << where << std::endl;
}
}
Keep in mind that when using read() you need to know how much to read because the function will not stop if it reaches eof or the buffer becomes empty.
E.g.:
1 2 3 4
MyClass myInstance;
input.read(reinterpret_cast<char*>(&myInstance), sizeof(MyClass));
if (input.fail())
std::cout << "failed to read binary data" << std::endl;
void testWrite(constchar fileName[])
Means that it gets an array of characters as parameter. Having the const in front of the type name means that the content of the array cannot be changed.
int n = 1;
has the same effect as int n = int(1);
Using int(1) will return an integer equal in value with the one specified as parameter. I only wrote it to make it clear that I'm outputting an integer and not something else.
numberOne and numberTwo are local int variables that I use to store what I read from file (int(1) and int(2) respectively).