get rid of warning: the use of `tmpnam' is dangerous, better use `mkstemp'

This is a discussion on get rid of warning: the use of `tmpnam' is dangerous, better use `mkstemp' within the C++ Programming forums, part of the General Programming Boards category; hi, i do know about the difference of tmpnam and mkstemp, but because i need to get a "free" name ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    23

    Question get rid of warning: the use of `tmpnam' is dangerous, better use `mkstemp'

    hi,

    i do know about the difference of tmpnam and mkstemp, but because i need
    to get a "free" name to open the filestream later by myself instead of the
    mkstemp that already opens the stream and creates the file as soon as it
    is called.
    is there a possibility to get rid of this warning, i mean, not on my computer
    but on computers of others who might compile the program?
    or is there a possibility to use a function like mkstemp but not create and
    open the file at the same time that does not give this warning?

    thanks,

    toby

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Use safe functions at the expensive of almost anything else.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    How are you intending to handle the possibility that between creating the name and using the name, that something else won't come along and use the same name?

    That is what mkstemp is designed to avoid, those kinds of race conditions.

    Besides, if you really need to know it's name, then perhaps mkstemp (and even tmpnam) is the wrong thing to be using. Perhaps roll your own unique name, and add in the current process ID. That stands a fair chance of being unique.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    If I understand correctly, the poster probably wants an fstream for the temp file, which poses an interesting problem indeed.
    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

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Your compiler probably has a #pragma to ignore certain warnings, but I agree with the others -- you should always try to use the most secure function available.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Secure versions are easy to get rid of if others want to compile your code with simple pseudo functions or defines, but it's not easy the other way around, so by all means - go for the safe functions!
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21