0

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++?

ghostmansd
  • 3,285
  • 5
  • 30
  • 44

3 Answers3

1

use the read or readsome function in istream. e.g

std::vector<char> buffer(2, 0);

if (myfile.read(&buffer[0], 2))
  std::copy(buffer.begin(), buffer.end(), std::ostream_iterator<int>(std::cout, ""));
Nim
  • 33,299
  • 2
  • 62
  • 101
  • Oh, that's why I prefer to use Python or try to write functions for it in C++ if Python is too slow. :-) – ghostmansd Oct 14 '11 at 07:50
  • @ghostmansd: I don't follow... the C++ code in this particular case is equivalent to the python code, with the only difference that you need to create the buffer up front. The `copy` is just a fancy way of printing the contents of the buffer, but if instead of a `vector` you use a string you can just `cout << str`. – David Rodríguez - dribeas Oct 14 '11 at 08:01
  • @ghostmansd, yeah I could have written a simple for loop to print byte-by-byte, but my fingers now automatically type the above.. ;) the key function of interest is the `read` (and `readsome`) – Nim Oct 14 '11 at 08:05
  • @ghostmansd, you are missing all the includes and anyway, AFAIK, you won't be able to do the file io stuff on the online compilers, you'll have to try this on your own machine... – Nim Oct 14 '11 at 08:16
  • I have included , and . I simply have copied only main function to pastebin. :-) – ghostmansd Oct 14 '11 at 08:21
1

Change the function definition to this ( notice the & sign):

string read_two_bytes(fstream & file)
vivek
  • 4,951
  • 4
  • 25
  • 33
1

@vivek has pointed out that you can't pass an fstream "by value". Passing things by value makes copies of them (or rather, runs their copy constructor, which may or may not actually make a "deep" copy of them).

Once you fix that, iostreams are actually cute and cuddly. They can detect the type you're asking for and read just that amount of data. If it's a char and you use the stream operators, it'll read a byte's worth:

string read_two_bytes(fstream& file)
{
  char byte1, byte2;
  file >> byte1 >> byte2;

  string two_bytes;
  two_bytes += byte1;
  two_bytes += byte2;

  return two_bytes;
}

@Nim seems to be trying to give you a generalized answer, perhaps to show off C++ vs Python. It's more answering the question for "N-bytes", except he hardcoded 2 so it just looks like overkill. It can be done easier, but nice to know the flexiblity is there...no?

If you're new to C++ I/O you might find the answer to this question I bothered to write the other day to be interesting as a contrast to the methods being suggested by other answers:

Output error when input isn't a number. C++

Community
  • 1
  • 1