Problem in Renaming of file???

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 294
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Problem in Renaming of file???

 
0
  #1
May 3rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,602
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1489
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Problem in Renaming of file???

 
0
  #2
May 3rd, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 294
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: Problem in Renaming of file???

 
0
  #3
May 3rd, 2008
  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,602
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1489
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Problem in Renaming of file???

 
0
  #4
May 3rd, 2008
what version of MS-Windows are you running? Use Windows Explorer to see if it will rename the file to that ip address.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 294
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: Problem in Renaming of file???

 
0
  #5
May 3rd, 2008
I m using Windows Xp.
There is a IP address in xyz file so i want to rename xyz file with that ip address....
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,602
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1489
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Problem in Renaming of file???

 
0
  #6
May 3rd, 2008
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;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 327
Reputation: Colin Mac is on a distinguished road 
Solved Threads: 22
Colin Mac Colin Mac is offline Offline
Posting Whiz

Re: Problem in Renaming of file???

 
1
  #7
May 3rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 294
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: Problem in Renaming of file???

 
0
  #8
May 3rd, 2008
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";
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 327
Reputation: Colin Mac is on a distinguished road 
Solved Threads: 22
Colin Mac Colin Mac is offline Offline
Posting Whiz

Re: Problem in Renaming of file???

 
0
  #9
May 3rd, 2008
Can you upload the xyz.txt here?
Last edited by Colin Mac; May 3rd, 2008 at 11:40 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 294
Reputation: Aamit has a little shameless behaviour in the past 
Solved Threads: 11
Aamit Aamit is offline Offline
Posting Whiz in Training

Re: Problem in Renaming of file???

 
0
  #10
May 5th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC