LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-23-2006, 03:57 AM   #1
christina_rules
LQ Newbie
 
Registered: May 2006
Posts: 25

Rep: Reputation: 15
how do i delete and rename my file in c++?


i've tried to use system("mv argv[1] argv[2]"); to rename the file but the error states as follows:

mv:error can't stat source argv[1].

i've entered a filename where i've the file in the directory. so what does the error mean and how can i rename a file? does the same applies to delete a file?
 
Old 06-23-2006, 04:23 AM   #2
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
Neat error ;-)

Unless the file you want to move is named "argv[1]" you'll have to get that error, of course. You have to build up your command (paramter of system) which consists of three parts:
1. the command itself (mv), followed by space
2. the first argument (value of argv[1]) followed by space
3. the second argument (value of argv[2]).

I hope you are able to get the rest on your own.

Last edited by Flesym; 06-23-2006 at 04:26 AM.
 
Old 06-23-2006, 06:03 AM   #3
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Using system for that is lame, when <cstdio> provides std::rename().
 
Old 06-23-2006, 10:27 PM   #4
christina_rules
LQ Newbie
 
Registered: May 2006
Posts: 25

Original Poster
Rep: Reputation: 15
ok i tried to change my codes to something like this

source = argv[1];
destination = argv[2];

then system("mv source destination");

i still get the same error. did i use the wrong step to get the value?
 
Old 06-23-2006, 10:33 PM   #5
christina_rules
LQ Newbie
 
Registered: May 2006
Posts: 25

Original Poster
Rep: Reputation: 15
Hivemind, i'm able to rename using the rename(). Thanks a lot but i still want to know how to fix it using system().
 
Old 06-24-2006, 12:05 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You need to build your string using a slightly different technique.
Code:
"mv source destination"
will give you the literal source not the value held in the variable source.
Code:
"mv " + source + " " . destination
This says, take the string literal "mv " and append to it the value held in source, then append the string literal " " and finaly append to it the value held in the variable destination.
This will work if source and destination are defined as being objects of the C++ class string. However if they are character arrays then you will need to look at a C style function to help you out. Maybe something like sprintf
Code:
sprintf()(result,"mv %s %s",source,destination)
This is where result is a character array large enough to hold the resultant string, and this will replace the first %s with the value held by the variable source and the second %s will be replaced by the value held in the variable destination.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot edit, delete, rename, or overwrite lilo.cof R_Shackleford Slackware 2 05-13-2006 05:44 PM
rename a file anoosh Programming 3 04-12-2006 02:15 PM
Tried to delete file as root but it says I don't have permission to delete it! beejayzed Mandriva 23 03-12-2004 02:46 AM
How do I rename a file? Thaidog Linux - Newbie 5 10-10-2003 10:28 PM
How to rename a file? juanb Linux - General 4 03-19-2003 11:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration