Thread: rename works intermittently

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

    rename works intermittently

    Hey folks!

    Code:
    if(!found)
    {
            strcpy(LineData[counter2].filename2, LineData[counter].storefilename);
            rewind(p_openfile);
    	insertstring();
    	counter2++;
    	fclose(p_openfile);
            remove(LineData[counter].storefilename);
    	rename("tempfile.txt", LineData[counter].storefilename);
    }
    I have about 80 files that are processed by the above code. In some instances, "tempfile.txt" does not get renamed to "LineData[counter].storefilename".

    I can verify that "tempfile.txt" exists, the file "LineData[counter].storefilename" has been deleted, and that "LineData[counter].storefilename" is a valid filename in the structure, using a debugger. So why does rename not work in all instances when it is called?

    Thanks in advance.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  2. #2
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Make sure that all file handlers and streams that point to LineData[counter].storefilename are closed . . . if you haven't already.

    Try rename() on some other files in other programs, and experiment.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    check the return value and on failure - exemine errno

    http://linux.die.net/man/2/rename
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anybody show me why this works??
    By tzuch in forum C Programming
    Replies: 2
    Last Post: 03-29-2008, 09:03 AM
  2. rename() problem
    By Dimeslime in forum C Programming
    Replies: 4
    Last Post: 02-09-2002, 05:38 PM
  3. Rename a value in the registry?
    By cyberpuck52 in forum C++ Programming
    Replies: 10
    Last Post: 01-22-2002, 09:58 PM
  4. rename a file on the C drive
    By cyberpuck52 in forum C Programming
    Replies: 3
    Last Post: 01-20-2002, 01:52 PM
  5. Problem with cgi script - can't rename files
    By bjdea1 in forum C Programming
    Replies: 2
    Last Post: 12-12-2001, 04:09 PM