Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

i want to copy a file from c: to d: using command line arguments

 i want to copy a file from c:\ to d:\, moreover if the file does not exist in d:\ then i want to create it there and then copy the contents into it ...i want to use commandline arguments for this
can the experts help !!!!
0
shwetaabhishek
Asked:
shwetaabhishek
  • 6
  • 6
  • 6
  • +3
1 Solution
 
sunnycoderCommented:
Hi shwetaabhishek,

to open a file for writing and create it if it does not exist, use open ("filename", O_CREAT|O_WRONLY)

to copy the file, you need to
1. open source file in read mode (fopen/open)
2. open destination file in write mode (fopen/open)
3. read from source and write to buffer (fread,fwrite/fgets,fputs)
4. close both the files (fclose)

What do you want to do with command line arguments here and is this a homework assignment ?

Sunnycoder
0
 
leflonCommented:
Hi shwetaabhishek,

there are different ways to do this
you may
1) use the system() function to call the OS specific copy command, there for you have to build a command string using your cmdline args.
2)  open the file on c: for reading
    open a file on d: for writing (maybe create it in this process, will an existing file be overwritten?)
    read input from file1 and write it to file2
    close both files

leflon

hth
leflon
0
 
ssnkumarCommented:
Do you want to write a C porgram, or will a shell script do?

-ssnkumar
0
NEW Internet Security Report Now Available!

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out this quarters report on the threats that shook the industry in Q4 2017.

 
anupvijayCommented:
Alternatively you can select the file in C:\ press CTRL + C and go to D:\ press CTRL + V
:-))
Sorry for that, just got too used to my job as as a software engineer :-))

Fun apart..........

Seeing C:\ and D:\ I assume you are on windows.

And seeing the problem at hands I think it is a college/school assignment.
So try converting what other experts have told, into an algorithm and finally a C program if that is what you want ultimately.
Command line arguments are supported in C through the arguments of "main" function named argv and argc.

Try to capitalize on these leads and try something and post back and all of us here will surely help you.

Cheers.
0
 
ssnkumarCommented:
I asked a question and it is selected as answer!
I am very much surprised!!!!

-ssnkumar
0
 
shwetaabhishekAuthor Commented:
thanks a lot , the thing is this is not an assignment or something actually i want to write a code for "sink" command
this command picks the file from database allow user to manipulate it and saves it giving it a version number.
0
 
anupvijayCommented:
Hi shwetaabhishek,

I am not aware of a "sink" command. But if you post the part of the code where you are stuck and give details of what
you want to do I am sure you will get to the solution quite soon.

Good Luck.
0
 
shwetaabhishekAuthor Commented:
here is the complete problem......
i want to update a file and give it a version number after updation
e.g test file is updated by user then save it on database as test1, ( 1 is the version number)everytime a user updates it i want increment the version number test2 ,test3 and so on
please help
0
 
ssnkumarCommented:
Is your problem still not solved!!
I thought, your problem got solved when you accepted one of the comments as answer!:-)

-ssnkumar
0
 
shwetaabhishekAuthor Commented:
i am sorry for that actually i am a new user don't know the rules
0
 
ssnkumarCommented:
Hi Shwetha,

The understanding is like this.
You will ask a question, telling the experts how much points she is going to give depending on the complexity of the problem.
Then, many comments will be posted (hopefully) by different experts.
Some expert may ask for clarification, or may give some suggestion.
You will have to constantly go on giving your feedbacks.
At one point you may feel that, your question is answered.
At that point, you have to select, whichever comment you think as best, as answer and then close the question.
Once you choose a comment as answer, the question is deemed as closed and no more comments will appear on that question! Nobody will try to answer a question which is already answered!!

Now, you have selected one comment as answer and with this it is assumed that your question is answered.
If you have selected it by mistake and if you still want to continue the discussion, then post a comment to the moderator (page editor), asking to deselect the answer and after that continue the interaction with experts.

Hope it is clear now:-)

-ssnkumar
0
 
sunnycoderCommented:
Hi shwetaabhishek,

I have again unaccepted the answer ... ssnkumar has already given a good overview (thanks!)
You can read the guidelines here
http://www.experts-exchange.com/help/closing.jsp

Back to your problem

How to copy the file has already been outlined
http:#10820895 
I guess the only remaining problem is that of renaming the files

If the prefix is constant e.g. test, then you can use sscanf to get the version number
sscanf ( filename, "test%d", &version );
version++; to increment the version
sprintf ( filename,"test%d", version ); will form the new filename which you can then use
0
 
shwetaabhishekAuthor Commented:
please deselect the answer
0
 
ssnkumarCommented:
Use:
fopen() to open a file.
fread() or fscanf() to read a file.
fwrite() or fprintf() to write to file.
fclose() to close the file.

Hope this helps.

-ssnkumar
0
 
shwetaabhishekAuthor Commented:
suppose test.txt is the file name,
after being used , edited by a user, when he saves the file the file should be saved as test.txt1
similarly for the second time when he uses it and saves it will be saved as test.txt2 and so on
0
 
ssnkumarCommented:
So you can do it like this.
i = 1;
FILE *fp;
while (1)
{
    sprintf(filename, "%s%d", filename, i);
   
    fp = fopen(filename, "r");
    if (fp != NULL)
    {
        fclose(fp);
        break;
    }

    i++;
}

So, when you come out of the loop, you have to correct filename and you can open it in write mode, write data into it and then close. With this the file with correct name is created.

-ssnkumar
0
 
leflonCommented:
shwetaabhishek,

just so i get it clear.
the prog the user uses to modify the file is the same whoich saves the file to a different file name? or are these two different progs (so you first edit it, say with notepad, and than use your prog to do the versioning)?

leflon
0
 
sunnycoderCommented:
If the prefix is constant e.g. test, then you can use sscanf to get the version number
sscanf ( filename, "test%d", &version );
version++; //to increment the version
sprintf ( filename,"test%d", version ); //will form the new filename which you can then use
0
 
Avik DasguptaCommented:
Here is a hint to this.
char * s=argv[1]; // ur file text.txt in command line
int l=strlen(s);
int version=0;
// if u have a single digit appended/modified then
if (modify_flag==0){s[l]=1;s[l+1]='\0';modify_flag=1;}
else{// has been modified prev.
  version=s[l-1]-'0';
  version++;
  s[l-1]=(char)(version+48);
}
// Now ur file is an enhanced version

>> i want to copy a file from c:\ to d:\
u can give the complete path in fopen argument

Hope that helps u,
Avik.
0
 
Avik DasguptaCommented:
Hi Sunnycoder,
  >> sscanf ( filename, "test%d", &version );
if the input comes from command prompt, and say original file name was test27, then how to handle the situation.
i.e. value of argv[1]  is test27

Avik.
0
 
Avik DasguptaCommented:
Furthermore it is also not a good practice to append the version number to the file name. Like if the original file was xyz.txt

>xyz.txt1 // not good
 
>xyz1.txt // good one

when working under DOS environment

Avik.
0
 
sunnycoderCommented:
>if the input comes from command prompt, and say original file name was test27, then how to >handle the situation
use sscanf on argv[1] instead of filename .... All that matters is reading the version
0
 
Avik DasguptaCommented:
>> All that matters is reading the version
I am referring that if the original file is named as test27 then its first modification will be test28, hence instead of appending the version to the file it is actually changing the filename totally. There must be a flag that will be giving the indication whether the file is the actual one or a modified one. I feel there in lies the catch of the problem.
   Moreover as I referred in my previous post one must not append the version as it will mess up with the extension and the program for openning the file will not be able to open up its versions.

Avik.
0
 
sunnycoderCommented:
>There must be a flag that will be giving the indication whether the file is the actual one or a modified one.
Not required ... The files have been modified in increasing order of names ... whether you change the extension or the name, either way filename gets changed ... as far as C is concerned there is no difference ... it is only human readability that is affected ....

Also a far better option than storing the whole files is to store the differences (delta) as is done in most commercial version control softwares
0
 
Avik DasguptaCommented:
>> whether you change the extension or the name, either way filename gets changed ... as far as C is concerned there is no difference ... it is only human readability that is affected ....

But the "compiler readability" will also be affected.
Suppose as a result of a Mutation Analysis 3 files are generated from an original file xyz.c (say) . If u use the rule of appending version information for different version generation then it will be xyz.c1 , xyz.c2 , xyz.c3. Now my question to u is how will the compiler gcc(say) use those files to compile them ?

Moreover there are some functionalities that vanishes due to messing of extensions. Will u be able to observe the syntax highlighting features in vi editor when u try to open a C/C++ file without its extensions, Fortran file without .for extension, Cobol file without .cob extension ,....

And for the modified flag issue, I said that because a file with an integer ending like help77 or temp1 will have problems when they start spanning new versions. Although a general approach can be deviced, but requires a little bit more programming effort.

Avik.
0
 
leflonCommented:
anybody heard anything from shwetaabhishek lately?? :-)

@Avik77 & @sunnycoder:
nice little discussion, is in danger of getting a little OT lately (i may have missed it, but who wants to compile the backup files), but interesting to listen to, no offense :-).

i still have aproblem who is editing what with what prog. is there aprog for editing an d asecond one for storing (versioning) the file, or is there just one? and what is the database the file is stored to? (and how do i handle this internal links, will take a look at the help)

cheers
leflon
0
 
Avik DasguptaCommented:
>> who wants to compile the backup files

There are situations when new versions or modified versions are not considered as backups but an integral part of the application. Thats why I provided the example of Mutation Analysis, which is a Software Testing Method. It is a method that involves comparison of the result of executing a given program on some known test inputs with the results of executing a number of marginally different and incorrect programs. Now generation of mutant programs will be the different versions. Certainly u need to compile those mutant programs !
Again when u need to process an input C program(say) to generate a variety of tranformations like decommenting, indenting, etc. What will u do with the modified ones, throw them away! or make them available for compilation.

Avik.
0
 
shwetaabhishekAuthor Commented:
i just request to clost this question
since i got the answer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

  • 6
  • 6
  • 6
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now