Thread: tmpnam() : permission denied when try to open temp file

  1. #1
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391

    tmpnam() : permission denied when try to open temp file

    Hey folks!

    Code:
    char tempname[FILENAMELENGTH];
    FILE *p_openfile1;
    
    tmpnam(tempname);
    printf("%s", tempname);
    
    if((p_openfile1 = fopen(tempname, "w")) == NULL) {
        perror("Error opening temp file"); 
        exit(EXIT_FAILURE);
    }
    When I try to open the filename for writing, the error message is:
    Code:
    \s2vc. Error opening temp file: Permission denied
    I have no problems opening any other file, just the temp file generated by tmpnam().

    I am assuming that fopen() tries to open the file in the "Windows" folder, which I don't have permission to do as a normal user. Does the fact that the temp filenames ends in a . have anything to do with the problem of opening it?

    EDIT: Actually, is \ a part of the filename too? Could that be causing problems?

    My textbook doesn't mention any such problem.

    Thanks in advance.
    Last edited by happyclown; 03-16-2009 at 11:02 PM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Look at the filename -- it starts with backslash. Looks like something is funny with your system so that it's not using the actual Windows temp directory.

    Most manuals for tmpnam() say very clearly "Do not use this function."
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Thanks brewbuck.

    In my textbook, the output[EDIT: printed in the book itself, not on my system] of a similar piece of sample code also has a \ in the temp filename that is generated.

    Perhaps this is a problem with tmpnam() and Windows systems in general.

    I'll try another function!

    EDIT: A quick search on google seem to indicate that only tmpnam is ANSi compatible for generating temp files(text). :/
    Last edited by happyclown; 03-16-2009 at 11:30 PM.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    tmpfile() is also standard. It gives you an open FILE. If you don't explicitly need the filename, just an open temporary file, that should work.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Invoking MSWord
    By Donn in forum C Programming
    Replies: 21
    Last Post: 09-08-2001, 04:08 PM