0

I found the length using seekg and tellg, then read them into an unsigned char*. The debugger is showing incorrect text though.

ifstream is (infile, ifstream::binary);
//find length
is.seekg (0, is.end);
int length = is.tellg();
is.seekg (0, is.beg);

char * buffer = new char [length];
is.read (buffer,length);
//delete[] buffer;  removed

//size_t cipherTextLength = length;                                removed    

//unsigned char* cipherText = new unsigned char[cipherTextLength]; removed

//is.read (reinterpret_cast<char*>(cipherText),length);            removed

edit:

text file is something like this:

.l F4"w2ÍögPl Ð    l œ›”  ÿÿÿPl (goes on)

debugger shows something like:

ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««þîþîþîþ

edit: now textfile only shows . l and debugger shows .\xl

Andrew Tsay
  • 1,543
  • 6
  • 18
  • 31
  • 5
    You're calling `is.read()` once to get the whole file, throwing that away, then wondering why the next read attempt doesn't do anything...? It's already at the end of the file, with `.eof()` true. Anyway, it's probably a better idea to [read the entire file content into a std::string](http://stackoverflow.com/questions/2602013/read-whole-ascii-file-into-c-stdstring?rq=1) - they release the memory in the destructor, handle binary content just fine, and have lots of useful functions to help you query and manipulate them. – Tony Delroy Apr 23 '15 at 04:32
  • @TonyD Both of Tyler McHenry's solutions only read the file until a nul. – Andrew Tsay Apr 23 '15 at 04:45
  • 1
    @atsay714 Can you fix your example above so you're not reading from the `ifstream` twice? That should work. That being said, you should use one of the solutions TonyD linked to so you're not managing memory manually. The `istreambuf_iterator` answer should read until EOF is reached, not till the first NUL character. Are you opening the file in binary mode? – Praetorian Apr 23 '15 at 04:50
  • @Praetorian I see `fstream::binary` in the specified openmode. And I would have thought binary was required to send to the stream rdbuf to squelch newline translation, but I honestly can't remember whether that xlat is at the rdbuf or the outermost get. – WhozCraig Apr 23 '15 at 05:00
  • @Praetorian Fixed the above code above, and added ifstream t(infile, ios::binary|ios::out ); to Tyler McHenry's solution and both stop at . l as shown above in my sample text file. opening the textfile in notepad++ shows SOH between . and l and NUL afterwards, so I'm guessing it's still the culprit – Andrew Tsay Apr 23 '15 at 05:01
  • 2
    You're not watching that var (`buffer`) in the *watch* or *vars* debug window, right? You're actually dropping it on the memory browser and seeing the extended bytes dump, correct? Because the formatter in the debugger will naturally stop displaying output for a `char*` once it thinks it found a terminator (which you *don't* want it to do). Try dragging `buffer` to the "address" box of a memory window and see whats there. (duh, btw, what debugger are you using? the previous instructions were for VS on windoze). – WhozCraig Apr 23 '15 at 05:07
  • 2
    @WhozCraig I was asking whether he was using `binary` in the example linked to in the first comment. Pretty sure you're right, you need `binary` to prevent newline translation. @atsay As WhozCraig says, you might be making the mistake of trusting debugger output. There are ways to force debuggers to display an entire array. On VisualStudio for instance, you can type `buffer,100` into the watch window to display `100` elements. Of course, you could just try to decrypt the data and see if you get the expected result. – Praetorian Apr 23 '15 at 05:10

2 Answers2

0

I think you are not adding \0 to the buffer. I did faced the same issue and solved it by adding +1 to the length.

char *buffer = new char [length + 1];
buffer[length] = 0; // Adding terminating character in content.
iFileStream.read(buffer, length);
std::string str(buffer);

Now the str should be correct. Hope this helps.

Pant
  • 1,426
  • 4
  • 20
  • 46
0

As your code if.read can get file binary. your program is string output except. you can print character on by on use %c from buffer

Liyuan Liu
  • 152
  • 2