wifstream crashes the program
This is a discussion on wifstream crashes the program within the C++ Programming forums, part of the General Programming Boards category; > 2. This will not fail because there is a statement len++ for the '
';
Except by doing so, you ...
-
and the hat of mystery
> 2. This will not fail because there is a statement len++ for the '\0';
Except by doing so, you step off the end of the memory you allocated, write a \0 into someone else's memory, then get abnormal program termination.
You need to allocate len+1 slots if you want to read len items, then append a \0.
-

Originally Posted by
Salem
Except by doing so, you step off the end of the memory you allocated, write a \0 into someone else's memory, then get abnormal program termination.
You need to allocate len+1 slots if you want to read len items, then append a \0.
What do you mean that I am writing outside my memory. I can't see it in the code. After len++ I allocate memory: len-1 for all tokens and the last one for '\0'. I don't see that I am writing '\0' in someone's else's memory.
And why is the program writing binary data, I have never declared ios_base::bin
-
Cat without Hat
read() and write() are for dealing in binary data, or bulk characters. But since you're casting, you don't have characters ready in the proper form, so it must be binary data.
All the buzzt!

CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
-
and the hat of mystery
> What do you mean that I am writing outside my memory.
Code:
int len = 10;
char *array = new char[len];
read( array, len ); // reads len chars, the array is full, no room for a \0
len++;
array[len] = '\0'; // oops, you lose, but maybe not immediately...
You might seem to get lucky on a few attempts, but sooner or later, this kind of code will cause a crash.
-
And how must I fix it, or is there a easier way to write and read wstrings and strings to a file?
Must I use operator<< and operator>>?
-
and the hat of mystery
Well if you allocated len+1, then read len, that might be a start.
-

Originally Posted by
Wicket
And how must I fix it, or is there a easier way to write and read wstrings and strings to a file?
Must I use operator<< and operator>>?
I already told you a solution, and so did CornedBee:

Originally Posted by
CornedBee
Writing binary data using the wide streams is simply wrong, end of story. Use the narrow streams if you want to deal in binary data.
And operators << and >> are strictly for text and not for binary.
But also try to fix the len problem as Salem mentions.

Originally Posted by
Adak
io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.

Originally Posted by
Salem
You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.
Outside of your DOS world, your header file is meaningless.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
-
At last, it is working.
Thanks everyone, not only for the functions to get working but also to understand some more things.
Popular pages Recent additions
Similar Threads
-
By spadez in forum C Programming
Replies: 1
Last Post: 03-23-2009, 04:16 PM
-
By Z'Ha'Dum in forum C Programming
Replies: 3
Last Post: 02-29-2008, 12:29 PM
-
By maxorator in forum C++ Programming
Replies: 1
Last Post: 10-01-2005, 06:39 AM
-
By ssjnamek in forum C++ Programming
Replies: 7
Last Post: 09-26-2005, 04:55 PM
-
By blackwyvern in forum C++ Programming
Replies: 3
Last Post: 01-27-2002, 11:28 PM