943,898 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2737
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
May 3rd, 2008
0

Problem in Renaming of file???

Expand Post »
I want to rename file with name of ip address like (192.168.1.55.txt)
In xyz.txt file have ip address.
I m finding this ip address but rename giving problem???
  1. #include <stdio.h>
  2. #include<conio.h>
  3.  
  4. int main ()
  5. {
  6. int result;
  7. char ipadd[20];
  8. FILE *pFile;;
  9. pFile = fopen ("xyz.txt" , "r");
  10. fseek(pFile,172,SEEK_SET);
  11. fgets (ipadd , 16 , pFile);
  12.  
  13. printf("Before Rename");
  14. printf("%s",ipadd); // it gives ip address in xyz.txt
  15. char oldname[] ="xyz.txt";
  16.  
  17. result= rename( oldname , ipadd ); //problem is here
  18. if ( result == 0 )
  19. puts ( "File successfully renamed" );
  20. else
  21. perror( "Error renaming file" );
  22. fclose (pFile);
  23. getch();
  24. }
This gives OutPut:->
Before Rename 192.168.1.55
Error renaming file: Permission denied


How to solve this problem ??
Last edited by Ancient Dragon; May 3rd, 2008 at 7:56 am. Reason: add code tags
Similar Threads
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is offline Offline
341 posts
since Apr 2008
May 3rd, 2008
0

Re: Problem in Renaming of file???

The file is still open. Close it first before trying to rename it. Move line 22 up to about line 16
Last edited by Ancient Dragon; May 3rd, 2008 at 7:59 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 3rd, 2008
0

Re: Problem in Renaming of file???

  1. #include <stdio.h>
  2. #include<conio.h>
  3. int main ()
  4. {
  5. int result;
  6. char ipadd[20];
  7. FILE *pFile;;
  8. pFile = fopen ("xyz.txt" , "r");
  9. fseek(pFile,172,SEEK_SET);
  10. fgets (ipadd , 16 , pFile);
  11. fclose (pFile);
  12. printf("Before Rename");
  13. printf("%s",ipadd); // it gives ip address in xyz.txt
  14. char oldname[] ="xyz.txt";
  15. result= rename( oldname , ipadd ); //problem is here
  16. if ( result == 0 )
  17. puts ( "File successfully renamed" );
  18. else
  19. perror( "Error renaming file" );
  20. getch();
  21. }
OutPut:-
Before Rename 192.168.1.55
Error renaming file: Invalid Argument
Last edited by Ancient Dragon; May 3rd, 2008 at 9:08 am. Reason: add code tags -- for the 2nd time
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is offline Offline
341 posts
since Apr 2008
May 3rd, 2008
0

Re: Problem in Renaming of file???

what version of MS-Windows are you running? Use Windows Explorer to see if it will rename the file to that ip address.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 3rd, 2008
0

Re: Problem in Renaming of file???

I m using Windows Xp.
There is a IP address in xyz file so i want to rename xyz file with that ip address....
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is offline Offline
341 posts
since Apr 2008
May 3rd, 2008
0

Re: Problem in Renaming of file???

fgets() add the '\n' to the end of the string if it exists in the file. So you need to strip it off before calling rename().
  1. fgets (ipadd , 16 , pFile);
  2. if( ipadd[strlen(ipadd)-1] == '\n')
  3. ipadd[strlen(ipadd)-1] = 0;
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 3rd, 2008
1

Re: Problem in Renaming of file???

fseek is causing the problem.

Make sure to return a value from main.
Also, learn how to use code tags - the yellow background behind your code.

Edit - fseek is probably causing the problem. Your code works fine if you create a new text file
and comment it out.
Last edited by Colin Mac; May 3rd, 2008 at 9:41 am.
Reputation Points: 78
Solved Threads: 22
Posting Whiz
Colin Mac is offline Offline
327 posts
since Sep 2006
May 3rd, 2008
0

Re: Problem in Renaming of file???

It's not working.....
By printing ipadd it gives 192.168.1.55
can we convert char array i mean like this
char abc[]="192.168.1.55";
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is offline Offline
341 posts
since Apr 2008
May 3rd, 2008
0

Re: Problem in Renaming of file???

Can you upload the xyz.txt here?
Last edited by Colin Mac; May 3rd, 2008 at 11:40 am.
Reputation Points: 78
Solved Threads: 22
Posting Whiz
Colin Mac is offline Offline
327 posts
since Sep 2006
May 5th, 2008
0

Re: Problem in Renaming of file???

Windows IP Configuration


Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . :

IP Address. . . . . . . . . . . . : 192.168.1.55

Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . : 192.168.1.1
Active Connections

Proto Local Address Foreign Address State
TCP 0.0.0.0:80 0.0.0.0:0 LISTENING
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING
TCP 0.0.0.0:443 0.0.0.0:0 LISTENING
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
TCP 0.0.0.0:990 0.0.0.0:0 LISTENING
TCP 0.0.0.0:2869 0.0.0.0:0 LISTENING
TCP 0.0.0.0:3306 0.0.0.0:0 LISTENING
TCP 0.0.0.0:3389 0.0.0.0:0 LISTENING
TCP 0.0.0.0:8888 0.0.0.0:0 LISTENING
TCP 127.0.0.1:1029 0.0.0.0:0 LISTENING
TCP 127.0.0.1:1039 127.0.0.1:1040 ESTABLISHED
TCP 127.0.0.1:1040 127.0.0.1:1039 ESTABLISHED
TCP 127.0.0.1:1041 127.0.0.1:1042 ESTABLISHED
TCP 127.0.0.1:1042 127.0.0.1:1041 ESTABLISHED
TCP 127.0.0.1:5679 0.0.0.0:0 LISTENING
TCP 127.0.0.1:7438 0.0.0.0:0 LISTENING
TCP 192.168.0.45:139 0.0.0.0:0 LISTENING
UDP 0.0.0.0:445 *
UDP 0.0.0.0:500 *
UDP 0.0.0.0:1043 *
UDP 0.0.0.0:1229 *
UDP 0.0.0.0:1344 *
UDP 0.0.0.0:1816 *
UDP 0.0.0.0:4500 *
UDP 0.0.0.0:8000 *
UDP 127.0.0.1:123 *
UDP 127.0.0.1:1297 *
UDP 127.0.0.1:1900 *
UDP 192.168.0.45:123 *
UDP 192.168.0.45:137 *
UDP 192.168.0.45:138 *
UDP 192.168.0.45:1900 *

This is my xyz.txt
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is offline Offline
341 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Taking int like character
Next Thread in C Forum Timeline: Sorting a list of numbers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC