changing file names

This is a discussion on changing file names within the C Programming forums, part of the General Programming Boards category; hi I'm trying to change the names of some files (all kinds of files)... I've been trying to use the ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    30

    changing file names

    hi

    I'm trying to change the names of some files (all kinds of files)...

    I've been trying to use the rename() function...
    I don't know why but the content of the file I am trying to manipulate dissapears when i call this function...and the names are not changed...

    any clue?

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Check the return value of the rename function. If it failed, you will get a nen-zero result.

    Other than that, post your code.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    ok I got in fact a negative value but why? I opened the file in "wb" mode...

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    My first instinct is to check that you have the proper permissions, but your compiler's documentation may have some sort of list of error values and their meanings, so check that as well. If that doesn't help, I'm fresh out of ideas.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Post some code. We're not the psychic network.
    Code:
    if( rename( oldfilename, newfilename ) == 0 )
    {
        printf("File renamed from %s to %s\n", oldfilename, newfilename );
    }
    else
    {
        printf("Failed to rename %s to %s\n", oldfilename, newfilename );
    }
    You're not trying to rename something you have already fopened are you by any chance?

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    its okay i think i found the error...i'm sending bytes from a client application to a server application and i made an indexing mistake when storing the bytes in a buffer...so basically i have wrong file names...

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    the file i'm accessing is fopened yes

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Quote Originally Posted by quzah
    You're not trying to rename something you have already fopened are you by any chance?
    The "but why? I opened the file in "wb" mode" statement leads me to believe that he did.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    30
    the file should not be opened? i thought it was supposed to be opened before being renamed...
    anyway here's a part of my code, just ignore the networking part..
    Code:
    if( (inputFile = fopen(file1, "wb")) == NULL)
    	{
    		response[0] = 0x40;
    		sendall(connectedSock, (char*)response,1);  //file not found
    
    	}
    	else
    	{
    		if( (fopen(file2,"rb")) == NULL)
    		{
    			fclose(inputFile);
    			printf("%d",rename(file1,file2));
    			response[0] = 0;
    			
    			sendall(connectedSock, (char*)response,1);   //change succesfull
    		}
    		else
    		{
    			response[0] = 0xA0;
    			fclose(inputFile);
    			sendall(connectedSock, (char*)response,1);  //could not perform change
    		}
    	}

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. You don't rename things you're currently using. Depending on the OS, it will freak out. (Just try this in Windows. Go open a file, say a music file, have it playing, and then try and move it some place.)

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM

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