Solved

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

Posted on 2004-04-13
29
345 Views
Last Modified: 2010-05-18
 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
Comment
Question by:shwetaabhishek
  • 6
  • 6
  • 6
  • +3
29 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10820895
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
 
LVL 7

Expert Comment

by:leflon
ID: 10820910
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10821096
Do you want to write a C porgram, or will a shell script do?

-ssnkumar
0
 
LVL 2

Expert Comment

by:anupvijay
ID: 10821626
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10821639
I asked a question and it is selected as answer!
I am very much surprised!!!!

-ssnkumar
0
 

Author Comment

by:shwetaabhishek
ID: 10821641
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
 
LVL 2

Expert Comment

by:anupvijay
ID: 10821705
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
 

Author Comment

by:shwetaabhishek
ID: 10821760
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10821770
Is your problem still not solved!!
I thought, your problem got solved when you accepted one of the comments as answer!:-)

-ssnkumar
0
 

Author Comment

by:shwetaabhishek
ID: 10821785
i am sorry for that actually i am a new user don't know the rules
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10821813
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10821842
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
 

Author Comment

by:shwetaabhishek
ID: 10821845
please deselect the answer
0
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10821854
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:shwetaabhishek
ID: 10821874
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
 
LVL 8

Expert Comment

by:ssnkumar
ID: 10821896
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
 
LVL 7

Expert Comment

by:leflon
ID: 10821919
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10821927
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
 
LVL 2

Accepted Solution

by:
Avik77 earned 50 total points
ID: 10836614
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
 
LVL 2

Expert Comment

by:Avik77
ID: 10836651
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
 
LVL 2

Expert Comment

by:Avik77
ID: 10836677
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10839566
>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
 
LVL 2

Expert Comment

by:Avik77
ID: 10844186
>> 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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10856558
>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
 
LVL 2

Expert Comment

by:Avik77
ID: 10861543
>> 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
 
LVL 7

Expert Comment

by:leflon
ID: 10861903
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
 
LVL 2

Expert Comment

by:Avik77
ID: 10862219
>> 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
 

Author Comment

by:shwetaabhishek
ID: 10865590
i just request to clost this question
since i got the answer
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now