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

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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, ""));

5 Comments

Oh, that's why I prefer to use Python or try to write functions for it in C++ if Python is too slow. :-)
@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.
@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)
@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...
I have included <iostream>, <fstream> and <string>. I simply have copied only main function to pastebin. :-)
1

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

string read_two_bytes(fstream & file)

1 Comment

If I try to print it with cout, it shows unsupported symbols. If I write it to file, it shows "ÿÿ".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.