1

I have a program that I need to read binary text into. I read the binary text via a redirection:

readData will be an executable made by my Makefile.

Example: readData < binaryText.txt

What I want to do is read the binary text, and store each character in the binary text file as a character inside a char array. The binary text is made up of 32 This is my attempt at doing so...

unsigned char * buffer;
char d;
cin.seekg(0, ios::end);
int length = cin.tellg();
cin.seekg(0, ios::beg);
buffer = new unsigned char [length];
while(cin.get(d))
{
  cin.read((char*)&buffer, length);
  cout << buffer[(int)d] << endl;
}

However, I keep getting a segmentation fault on this. Might anyone have any ideas on how to read binary text into a char array? Thanks!

user200081
  • 563
  • 2
  • 12
  • 24
  • I said binary text because I'm not exactly reading from a binary file.. but simply redirecting the text inside the binary file as input into my program – user200081 Dec 03 '12 at 00:56
  • By convention "binary" and "text" are usually used as mutually exclusive descriptions of file contents. Not because you can't write a binary block into a "text" file or plain text strings into a "binary" file, but because mixing the modes is rarely useful. So when you say *"binary text file"* or *"the text inside the binary file"* we're left scratching our heads. NB: All files are stored in binary format, but in "text" files all the contents are to be treated as text. – dmckee --- ex-moderator kitten Dec 03 '12 at 01:05

4 Answers4

0

I'm more a C programmer rather than a C++, but I think that you should have started your while loop

while(cin.get(&d)){
Frank Riccobono
  • 1,043
  • 6
  • 13
0

The easiest would be like this:

std::istringstream iss;
iss << std::cin.rdbuf();

// now use iss.str()

Or, all in one line:

std::string data(static_cast<std::istringstream&>(std::istringstream() << std::cin.rdbuf()).str());
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

Something like this should do the trick. You retrieve the filename from the arguments and then read the whole file in one shot.

const char *filename = argv[0];
vector<char> buffer;

// open the stream
std::ifstream is(filename);

// determine the file length
is.seekg(0, ios_base::end);
std::size_t size = is.tellg();
is.seekg(0, std::ios_base::beg);

// make sure we have enough memory space
buffer.reserve(size);
buffer.resize(size, 0);

// load the data
is.read((char *) &buffer[0], size);

// close the file
is.close();

You then just need to iterate over the vector to read characters.

charles
  • 55
  • 8
0

The reason why you are getting segmentation fault is because you are trying to access an array variable using a character value.

Problem:

buffer[(int)d] //d is a ASCII character value, and if the value exceeds the array's range, there comes the segfault.

If what you want is an character array, you already have that from cin.read()

Solution:

cin.read(reinterpret_cast<char*>(buffer), length);

If you want to print out, just use printf

printf("%s", buffer);

I used reinterpret_cast because it thought it is safe to convert to signed character pointer since most characters that are used would range from 0 ~ 127. You should know that character values from 128 to 255 would be converted wrongly.

Tyson90
  • 106
  • 8