I'm currently trying to make analog of Python's function:
def read_two_symbols(fdescr):
return(file.read(2))
myfile = open('mytext.txt', 'rb')
two_symbols = read_two_symbols(myfile)
print(two_symbols)
Is there any way to do it in C++? That's what I've tried:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
string read_two_bytes(fstream file)
{
string byte1, byte2;
byte1 = file.get();
byte2 = file.get();
string two_bytes = byte1 + byte2;
return two_bytes;
}
int main()
{
fstream myfile("mytext.txt", ios_base::in | ios_base::binary);
string two_bytes = read_two_bytes(myfile);
cout << two_bytes << endl;
return 0;
}
However it fails. :-( How can I do it using C++?