473,513 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to use rename(); correctly.

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.

Feb 5 '06 #1
8 1946
Merlin wrote:
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

--
First Law of Bicycling:
No matter which way you ride, it's uphill and against the
wind.

Feb 5 '06 #2
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.

Feb 5 '06 #3
Merlin wrote:
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

--
"A radioactive cat has eighteen half-lives."

Feb 5 '06 #4
Merlin wrote:
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.

Feb 5 '06 #5
santosh wrote:
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.

Feb 5 '06 #6
santosh wrote:
Merlin wrote:
rename("testfile.txt","%d.zip");


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().
system("pause");


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
--
While having never invented a sin, I'm trying to perfect several.

Feb 5 '06 #7
On 5 Feb 2006 09:18:28 -0800, in comp.lang.c , "Merlin"
<mr*****@gmail.com> wrote:
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
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 5 '06 #8
"Merlin" <mr*****@gmail.com> writes:
[...]
#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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 6 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
9391
by: Tom | last post by:
I'm having a problem using a path with spaces as a parameter to os.rename() in a program on WinXP. This works fine at the command line (where the folder "c:\aa bb" exists) > os.rename( "c\aa...
3
6044
by: Saradhi | last post by:
Hi All, Here I am facing a performance problem with the TreeView Node renaming. I am displaying a hierarchy Data in a treeview in my Windows C# Application. My tree view represents an...
1
1986
by: Jay at SCA | last post by:
I've been having the following issue with a windows service I've created in vb.net (.net fw 1.1): Basically, my service monitors a folder for the creation of any new files of a particular type...
2
2942
by: Bruce Russell | last post by:
This may sound stupid but I can't rename the WebForm1.aspx in the solution explorer. The file is located in my local web server at C:\Inetpub\wwwroot\Lab3-VB-Starter\WebForm1.aspx Is there...
6
1431
by: Larry Woods | last post by:
I am trying to name my submenus (MainMenu control) and they show up in the menu dropdown...like they are O.K., but when I check my controls the names are still "MenuItemX". OTOH, the top-level...
6
2514
by: shuaishuaiyes | last post by:
Hello everyone... I'm a Chinese student and my English is very poor...So excuse me if I make grammar mistake. I want to ask some questions about "rename". I'm a beginner, so my C ..... :) I...
1
1968
by: jonathan184 | last post by:
trying to rename filenames and extensions then add a header in line1 of each file if the header existed in line 1 to go to the next file. but i am getting error explciti errors Here is my...
1
5709
by: cheesey_toastie | last post by:
I have a long query which I have set off and would like to stop, and rename one of the tables used. My query is due to my lack of understanding of the underlying structure of MSSQL-Server... ...
7
16131
by: jeddiki | last post by:
Hi, As I am in Turkey at present, I can not see vidoes on youtube. So I have tried a few proxies but keep finding them slow or not working. So I have installed myphpProxy on my server under...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5084
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.