Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Using system Function call in HP UNIX

 
SOLVED
Go to solution
Highlighted
CA1490051
Frequent Advisor

Using system Function call in HP UNIX

Hi All,

I want to rename a file in a C++ Program.
So i am using the system function call as follows .

int amin()
{
string src="Test.txt";
string Dest = "Test_1.txt"
system("mv Src Dest");
}
But if i execute the program the error message saying Src no such file or directory is displayed.

I also tried to do this by calling a shell script in the system function call as follows
int amin()
{
string src="Test.txt";
string Dest = "Test_1.txt"
system("./Move.sh Src Dest");
}

Move.sh is as follows
mv $1 $2

But, again the same error.

Can some one help me to achive this.

thanks in advance
Vikram
11 REPLIES 11
Highlighted
Torsten.
Acclaimed Contributor

Re: Using system Function call in HP UNIX

Your system is trying to do exactly what you wrote:

system("mv Src Dest");

# mv Src Dest

Do you have a file called "Src"?
No? Precisely.


Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Highlighted
James R. Ferguson
Acclaimed Contributor

Re: Using system Function call in HP UNIX

Hi:

Does your environment's PATH reflect where your files reside?

For that matter, why write a C or C++ program only to call 'system()' to rename a file? You should call 'rename()' [assuming that the source and destination reside within the same filesystem].

Regards!

...JRF...
Highlighted
Hein van den Heuvel
Honored Contributor

Re: Using system Function call in HP UNIX

Dear Vikram,

STOP!

Take your fingers from the keyboard and slowly turn around.
Walk to the nearest bookshelf and pick up a basic programmign book.
Any language.
even K&R C would be a good start.
Focus on VARIABLES.
Spend a few quality hours with the books. Now turn back.

But don't touch that compiler just yet.
Look around some first, notably with the 'man' pages.

Specifically type, and read,
man rename
man sprintf

Welcome to the wonderous world of programming!

Regards,
Hein.



Highlighted
Torsten.
Acclaimed Contributor
Solution

Re: Using system Function call in HP UNIX

...
if ( rename(Src, Dest) == 0 )
printf("%s renamed to %s\n", Src, Dest);
else
perror("rename");

return 0;
...

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Highlighted
CA1490051
Frequent Advisor

Re: Using system Function call in HP UNIX

Hi All,

I can rename the file using rename(). But, now i want it to tar and zip it.

Can any one tell me how to achive this?

system("tar -cvf file.tar file");

Obviously the shell will complain that file not found.
Where file for me is a string variable
initialised to "Temp.txt";

thaks and regards
Vikram
Highlighted
Torsten.
Acclaimed Contributor

Re: Using system Function call in HP UNIX

As mentioned above (directly and indirectly) - learn to work with variables!

From your initial question:

system("mv Src Dest");

Src and Dest are **NAMES** - not variables from the system point of view!

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Highlighted
CA1490051
Frequent Advisor

Re: Using system Function call in HP UNIX

Hi All,

Yes i know these are variables. But, still there is a requirement that i should achive this.

Any way to do this will be great help for us.....

thanks and regards
Vikram
Highlighted
Torsten.
Acclaimed Contributor

Re: Using system Function call in HP UNIX

"Yes i know these are variables"

Really? But you don't handle them as variables.

man system:

SYNOPSIS
#include

int system(const char *command);
...

You need to *build* the command string first!

Hope this helps!
Regards
Torsten.

__________________________________________________
There are only 10 types of people in the world -
those who understand binary, and those who don't.

__________________________________________________
No support by private messages. Please ask the forum!

If you feel this was helpful please click the KUDOS! thumb below!   
Highlighted
James R. Ferguson
Acclaimed Contributor

Re: Using system Function call in HP UNIX

Hi (again) Vikram:

> Yes i know these are variables. But, still there is a requirement that i should achive this.

This seems like a purely academic exercise. I agree with both Hein and Torsten's comments which are meant to be helpful. The use of 'system()' to perform shell tasks in a C program says that you may need to rethink your objective and your use of tools.

Regards!

...JRF...