Trying to use rename(); correctly.

M

Merlin

Ok....

I feel really dumb on this one, because I had previously figured it
out, and now don't have a clue.

I'm trying to get the user to input a new filename (via an integer
variable) and have that variable passed to rename, so that the file can
be named whatever the user specifies in the console window....

Currently using

#include <stdio.h>

main()
{
int filename;
printf("Please input a new filename");
scanf("%d",filename);
rename("testfile.txt","%d.zip");
system("pause");
}

but obviously doesn't work. Example assumes that the prog. is being run
from the same working folder as the file in question.

This is just the experiment that I was running. Don't know how to make
this work. Any help would be greatly appreciated.

Sorry the questions are so basic...if anyone knows of a better group,
let me know, and I'll post there.

Merlin.
 
V

Vladimir S. Oka

Merlin said:
Ok....

I feel really dumb on this one, because I had previously figured it
out, and now don't have a clue.

I'm trying to get the user to input a new filename (via an integer
variable) and have that variable passed to rename, so that the file

I can't really understand why you'd want a file name entered as an
integer, but hey, I've seen weirder...
can be named whatever the user specifies in the console window....

Currently using

#include <stdio.h>

main()

It's better to spell this out:

int main(void)
{
int filename;
printf("Please input a new filename");

Without terminating '\n' you risk not actually getting anything output.
scanf("%d",filename);

You certainly meant:

scanf("%d", &filename);

Also, don't use scanf(). Use fgets()/sscanf() combination instead.
Search this newsgroup for discussion why.
rename("testfile.txt","%d.zip");
^^
And how did you expect this to be populated? This is most likely why
your code did not work (apart from other things I pointed out which may
make it /maybe/ not work).

Use sprintf() or snprintf() to create your filename string, and then
pass that to rename().
system("pause");

This is highly non-portable -- system() /is/ standard function, biut
whatever you pass to it is not.

Some general notes:

Test for success of various operations. E.g. rename() will return
non-zero if renaming failed; scanf() and friends will return the number
of arguments successfully parsed, etc.

Horizontal /and/ vertical whitespace is cheap.

There may be other things, and what I wrote may be liable to correctins
by people more pedantic than I am. ;-)

Cheers

Vladimir
 
M

Merlin

Thank you so much Vladimir.

Like I said, I'm very new, and still figuring out how everything works.
Your comments and suggestions are greatly appreciated. I currently
have a very "non-portable" solution implemented. Essentially, I've
populated a temporary DOS batch file, and then used the batch file to
rename the file in question. In this particular case, I don't need a
super portable solution, but I will print off your replies here, and
work towards a much better, and more portable solution....

Thanks so much,

Merlin.
 
V

Vladimir S. Oka

Merlin said:
Thank you so much Vladimir.

You're very much welcome.

If you want more help, and less flak from this newsgroup (a /very/ good
idea), please also read this:

http://cfaj.freeshell.org/google/

for general posting guidelines (in a nutshell: don't top post, and quote
relevant context to what, and who you're replying to), as well as:

http://www.c-faq.com/

It is considered good manners to first look up any question you may have
in the C FAQ, and post it here only if you can't find or understand the
answer. A good C reference manual or textbook are also highly
recommended (searching this newsgroup will provide links to some).
Essentially, I've
populated a temporary DOS batch file, and then used the batch file to
rename the file in question.

I don't think there's anything wrong with using OS command interpreter
to do OS specific stuff.
In this particular case, I don't need a
super portable solution,

You can't have a 100% portable Cprogram doing the same thing, and the
part of it that isn't would have to be re-written for every new OS. If
non-portable stuff is all you need, and there are more convenient ways
of achieving them, just go ahead.

Cheers

Vladimir
 
S

santosh

Merlin said:
Ok....

I feel really dumb on this one, because I had previously figured it
out, and now don't have a clue.

I'm trying to get the user to input a new filename (via an integer
variable) and have that variable passed to rename, so that the file can
be named whatever the user specifies in the console window....

It might be better to get a line of input using fgets(), check the
string to determine if the characters in it match your filename
requirements and if so, use it to rename the file. This is more work,
but also more robust.
Currently using

#include <stdio.h>

main()

'int main(void)' and 'int main(int argc, char *argv[])' are the forms
that are standard compliant.
{
int filename;
printf("Please input a new filename");
scanf("%d",filename);

You must pass the address of 'filename' to scanf, not it's value, which
will be interpreted as a (faulty) address, leading to undefined
behaviour.
rename("testfile.txt","%d.zip");

The argument for the '%d' conversion specifier is also missing here.
system("pause");

As others have pointed out, use of system() is not portable.
Sorry the questions are so basic...if anyone knows of a better group,
let me know, and I'll post there.

As long as your program/question (or atleast a subset of it), involves
Standard C this is an excellent place to ask your questions.
 
S

santosh

santosh said:
As long as your program/question (or atleast a subset of it), involves
Standard C this is an excellent place to ask your questions.

Correction before I get flamed ;) :

As long as your program, (or the relevant portion of it), or question
involves Standard C, this is an excellent place to ask away.
 
V

Vladimir S. Oka

santosh said:
The argument for the '%d' conversion specifier is also missing here.

No! The rename function is defined as (C&V 7.19.4.2):

int rename(const char *old, const char *new);

The rename function causes the file whose name is the string pointed to
by old to be henceforth known by the name given by the string pointed
to by new (C&V 7.19.4.2.2).

OP needs to construct `new` (probably using sprintf() and friends)
before calling rename().
As others have pointed out, use of system() is not portable.

This is, actually one of the quirks of Standard C. It is guaranteed that
using system() is standard and portable. The weird thing is that what
you pass to it is almost guaranteed not to be.

I guess that a better way of using it would be to pass something created
either at compile time (a macro) or run time (a result of calling a
function) and make that system-dependent. E.g. (admittedly not
necessarily very nice):

#if OS_ID == MS_DOS
#define OS_PAUSE ("PAUSE")
#elif OS_ID == SM_SOD
#define OS_PAUSE ("STOP")
#else
#define OS_PAUSE ("")
#endif

...

system(OS_PAUSE);

Note also that if you pass NULL to system() it will return
non-zero /only/ if the underlying OS actually has a command processor.
This can be used for even more error checking, e.g.:

int os_has_cmd_proccessor;

...

os_has_cmd_processor = system(NULL);

...

if (os_has_cmd_processor)
system(OS_PAUSE);

BTW, in snippets above I have omitted all required #includes.

Cheers

Vladimir
 
M

Mark McIntyre

I'm trying to get the user to input a new filename (via an integer
variable) and have that variable passed to rename, so that the file can
be named whatever the user specifies in the console window....

filenames are normally strings, not numbers. int and %d are for
integers.

Try reading in a string with fgets(), then using sprintf() to create a
string with the new filename, and finally using rename() to change the
name.
Mark McIntyre
 
K

Keith Thompson

Merlin said:
#include <stdio.h>

main()
{
int filename;
printf("Please input a new filename");
scanf("%d",filename);
rename("testfile.txt","%d.zip");
system("pause");
}

Apart from the other errors that have been pointed out, just remember:
don't try to use printf-like format strings like "%d" with functions
that don't expect them. The arguments to rename() and system() are
just strings, not format strings. A common technique is to use
sprintf() or snprintf() to build a string, and then pass that to
another routine.

Allocating the proper number of bytes for the string is non-trivial.
snprintf() with a null pointer as its first argument tells you how
many characters are needed, but that may not be available in all
implementations (it's new in C99).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top