Thanks a lot for noticing the mistakes! It's been a while since I've last tried programming...
I corrected them in my program but it still displays only values of "-1" and the names don't change. I bypassed the check for the existence of the file (I'm not familiar with the necessary instructions for this), I hope it wasn't essential.
Also, thanks for the "sprintf" tip.
I have to admit I don't really understand C, but I've tried your code too with the specified modifications; line 5 gave me errors on compiling (I don't think my C++ supports "bool"), so I modified it to "int" and no more errors appeared, but it still doesn't do the trick, the file names stay the same. It doesn't display any error message (like the one you put on line 30).
I've also tried the tutorials on this site for the stuff I don't understand in your code, but I didn't really get them.
Although I changed the directory to the folder with the files (my version of C++ automatically puts the .CPP and the .EXE in the installation folder), could there still be a problem from this side? I've double-checked that the "oldname" is correct.
Here is the corrected code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<fstream.h>
void main()
{clrscr();
char oldname[200], newname[200];
for(int i=5;i<55;i++)
{sprintf(oldname,"%s%u%s",
"Emma Watson Ultimate Sexy Pics [Entertainclub.blogspot.com] (",
i,").jpg");
sprintf(newname,"%s%u%s","Emma Watson ",i,".jpg");
cout<<oldname<<endl<<newname<<" ";
//I've also put this "cout" to make sure the names are ok this time
cout<<rename(oldname,newname)<<" "<<endl;
}
getch();
}
|
Also, if it's any help, here is your code which I modified:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <stdio.h>
#include <stdlib.h>
int fileexists ( const char* filename ) //turned "bool" to "int"
{int ok;
FILE* fp = fopen( filename, "r+" );
ok = (fp != NULL);
if (ok) fclose( fp );
return ok;
}
int main()
{
char oldname[ 100 ];
char newname[ 100 ];
for (unsigned n = 5; n < 55; n++)
{
sprintf( oldname, "%s%u%s", "Emma Watson Ultimate Sexy Pics [Entertainclub.blogspot.com] (", n, ").jpg" );
sprintf( newname, "%s%0*u%s", "Emma Watson ", 2, n, ".jpg" );
if (fileexists( oldname ))
{
if (rename( oldname, newname ) == 0)
{
puts( newname );
}
else
{
printf( "FAILED to rename \"%s\"\n", oldname );
}
}
}
puts( "done." );
return 0;
}
|