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???

#include <stdio.h>
#include<conio.h>

int main ()
{
      int result;
      char ipadd[20];
      FILE *pFile;;
           pFile = fopen ("xyz.txt" , "r");
           fseek(pFile,172,SEEK_SET); 
           fgets (ipadd , 16 , pFile);
                   
           printf("Before Rename");
           printf("%s",ipadd);         // it gives ip address in xyz.txt
           char oldname[] ="xyz.txt";
                 
      result= rename( oldname , ipadd ); //problem is here
      if ( result == 0 )
      puts ( "File successfully renamed" );
      else
      perror( "Error renaming file" );
      fclose (pFile);
   getch();
}

This gives OutPut:->
Before Rename 192.168.1.55
Error renaming file: Permission denied


How to solve this problem ??

The file is still open. Close it first before trying to rename it. Move line 22 up to about line 16

#include <stdio.h>
#include<conio.h>
int main ()
{
int result;
char ipadd[20];
FILE *pFile;;
pFile = fopen ("xyz.txt" , "r");
fseek(pFile,172,SEEK_SET);
fgets (ipadd , 16 , pFile);
fclose (pFile);
printf("Before Rename");
printf("%s",ipadd); // it gives ip address in xyz.txt
char oldname[] ="xyz.txt";
result= rename( oldname , ipadd ); //problem is here
if ( result == 0 )
puts ( "File successfully renamed" );
else
perror( "Error renaming file" );
getch();
}

OutPut:-
Before Rename 192.168.1.55
Error renaming file: Invalid Argument

what version of MS-Windows are you running? Use Windows Explorer to see if it will rename the file to that ip address.

I m using Windows Xp.
There is a IP address in xyz file so i want to rename xyz file with that ip address....

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().

fgets (ipadd , 16 , pFile);
if( ipadd[strlen(ipadd)-1] == '\n')
    ipadd[strlen(ipadd)-1] = 0;

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.

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";

Can you upload the xyz.txt here?

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

fseek is causing the problem..

Yes, that is the major problem.
Here is the entire line that he is trying to read Before Renamesk . . . . . . . . . . . : 255.255.255.0

#include<conio.h>
#include <stdio.h>
#include "winsock2.h"
#include<iostream>
#include<fstream>

using namespace std;
void Myfun(string);
void Myfun(string a)
{
       int i,result;
       printf("\n In function");
       cout<<a;
       char oldname[] ="c:\\xyz.txt";
       char mychar[a.size()+1];
       std::strcpy(mychar, a.c_str());
     
      for(i=0;i<a.size()+1;i++)
     {
      printf("\n%c",mychar[i]);                     
     }
     
      result = rename(oldname,mychar);
      if ( result == 0 )
      puts ( "File successfully renamed" );
      else
      perror( "Error renaming file" );
      
}
  
int main ()
{
      int result,size;
      char firstipadd[20];
      string str,aaa;
      FILE *pFile;;
           pFile = fopen ("c:\\xyz.txt" , "r");
           fseek(pFile,172,SEEK_SET);
           fgets (firstipadd , 16 , pFile);
            rewind (pFile);
                      
    int p = fclose (pFile);
    if( p ==0)
    {
        printf("\nFile closed");
    }

                 
            for(int i = 0; firstipadd[i]!='\0'; i++)
          {
            str+= firstipadd[i];
            
          }
           cout<<"\n"<<str;
         
         Myfun(str);
        getch();
}

Here I m got ip address from xyz.txt
rewind it and successfully close it and transfer to function ... but when rename it .... it gives invalid Argument error ....
can string not correctly convert to char array??

#include <stdio.h>
#include<string.h>
int main ()
{
      char firstipadd[20], fname[30] = "D:\\";
      FILE *pFile;;
      pFile = fopen ("D:\\xyz.txt" , "r");
      fseek(pFile,169,SEEK_SET);
      fgets (firstipadd , 14 , pFile);
      fclose (pFile);
      strcat ( fname, firstipadd) ;
      rename ( "D:\\xyz.txt", fname ) ;
      getchar ( ) ;
}

This code is working for me for your txt file.
However, I dont like the idea of using fseek like this.
I would prefer reading the file char by char.
But since you are in a hurry, I'll give this for now.

#include <stdio.h>
#include<string.h>
#include<conio.h>
using namespace std;

int main ()
{
char firstipadd[20], fname[30] = "C:\\" ;
int result;
FILE *pFile;;
char *aaa;
pFile = fopen ("C:\\xyz.txt" , "r");

fseek(pFile,172,SEEK_SET);
fgets (firstipadd , 15 , pFile);
printf("\n%s",firstipadd);
fclose (pFile);
printf("%s",fname);

strcat ( fname,"\\") ;
strcat ( fname, firstipadd);
printf("\n%s",fname);

result=rename ( "C:\\xyz.txt",fname ) ;
if ( result == 0 )
puts ( "File successfully renamed" );
else
perror( "Error renaming file" );

getch();

}

Giving error:->

192.168.1.55
C:\
C:\\192.168.1.55
Error renaming file: Invalid argument

>>strcat ( fname,"\\") ;

remove this line, you already have a slash in the name

Hey and also u are reading \n along with the IP address, It is evident in the output
12 will do I guess

I got in fname --->C:\\192.168.1.55
but facing problem that how to this string giving in " " means
"C:\\192.168.1.55"

I dont understand your reply.

print the fname & show me what it prints

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.