Like I want to make a copy of yar.txt how do I use that filename and add a .cpy to it?
Like I want to make a copy of yar.txt how do I use that filename and add a .cpy to it?
Read this thread on the same topic.
How do I make a program with two arguments in dos
Tim S.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
Assuming you have two strings, one with the filename, and the other with the new extension, you could always copy both strings into a third string with strncpy().
Method one...
Method 2...Code:char NewName[255] = {0}; strcpy(NewName,OldName); strcat(NewName,".cpy"); // result: filename.txt.cpy
Code:char NewName[255] = {0}; char* ptr; strcpy(NewName,OldName); ptr = strchr(NewName,'.'); strcpy(ptr,".cpy"); // result... filename.txt becomes filename.cpy
thanks
I am trying it this way:
but it isn't working, it compiles ok but when run with the filename after the exe as test.txt it just doesn't workCode:char NewName[250] = {0}; char Oldname[250]={argv[1]}; strcpy(NewName,Oldname); strcat(NewName,".cpy");
That should generate warnings/errors for your compiler. If not, crank up the warning settings.
Then, you need to strcpy argv[1] into Oldname. Also, you should pick a consistent formatting for variable names. Either capitalize the 'N' in "Name" for NewName and OldName or don't, but be consistent.
Can you be a little more specific.
Do you want to copy the filename or the contents of the file?
copy both but make the copy of the file have an extra extension on the end like .cpy
You don't need to save argv[1], you can use that throughout your program. Here's another thing you can try, without length limits on the filename. You will need to add file operations and use new_name for the file you write to.
Code:char *new_name = malloc(strlen(argv[1]) + 1 + 4); /* +1 for \0, +4 for .cpy */ strcpy(new_name, argv[1]); strcat(new_name, ".cpy"); puts(new_name); free(new_name);
Some psuedocode:
Remember: they're called file streams for a reason. When you read a byte from an input stream (or write a byte to an output stream), it will automatically advance you to the next character, so you don't overwrite the last character you wrote, and you don't read the same byte again. This is much more efficient than copying the entire input file into memory, and writing the entire output file from memory.Code:int main(int argc, char *argv[]) if argv[1] is null, printf usage message argv[1] will be file to be copied FILE *in = fopen argv[1], mode "r" FILE *out = fopen argv[1] with ".cpy" added on, mode "w" int x; while ((x = fgetc(in)) != EOF) { fputc(x, out) } printf("SUCCESS!!!\n");
Refer to the other posts in this thread for working with strings.
Originally Posted by The Jargon File
This is what happens when you do scoop and poop coding... I gave you an EXAMPLE... not a finished product.
Get the idea... I gave you a general suggestion... you need to expand and modify it to your needs.Code:int main ( int argc, char* argv[] ) { char OutputFile[256] = {0}; // part of your program code here strcpy(OutputFile, argv[1]); strcat(outputFile,".cpy"); fp = fopen(OutputFile,"w"); if (! fp) { printf("Could not open %s",OutputFile); exit(-1); } // rest of program here return 0; }
Last edited by CommonTater; 01-10-2012 at 09:39 PM.
thanks