Thread: wifstream crashes the program

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #17
    Registered User
    Join Date
    Aug 2007
    Posts
    13
    Quote Originally Posted by Salem View Post
    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

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    Registered User
    Join Date
    Aug 2007
    Posts
    13
    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>>?

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you allocated len+1, then read len, that might be a start.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Wicket View Post
    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:

    Quote Originally Posted by CornedBee View Post
    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.
    Quote Originally Posted by Adak View Post
    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.
    Quote Originally Posted by Salem View Post
    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.

  8. #23
    Registered User
    Join Date
    Aug 2007
    Posts
    13
    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 subscribe to a feed

Similar Threads

  1. Simple login program - Crashes :(
    By spadez in forum C Programming
    Replies: 1
    Last Post: 03-23-2009, 04:16 PM
  2. Replies: 3
    Last Post: 02-29-2008, 01:29 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. program crashes on closure.
    By ssjnamek in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2005, 04:55 PM
  5. My program crashes with this code
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-28-2002, 12:28 AM