Right now, I have something like this…
CMD console window:
c:\users\username\Desktop> wrapfile.txt hello.txt
Hello
How would I get something like this?
CMD console window:
c:\users\username\Desktop> wrapfile.txt hello.txt hi.txt
Hello Hi
with this code?
#include <stdio.h>
#include <stdlib.h>
int main(int argc[1], char *argv[1])
{
FILE *fp; // declaring variable
fp = fopen(argv[1], "rb");
if (fp != NULL) // checks the return value from fopen
{
int i;
do
{
i = fgetc(fp); // scans the file
printf("%c",i);
printf(" ");
}
while(i!=-1);
fclose(fp);
}
else
{
printf("Error.\n");
}
}
Well, first of all: in your
maindeclaration, you should useint main(int argc, char* argv[])instead of what you have right now. Specifying an array size makes no sense when declaring anexternvariable (that’s what argv and argc are). On the top of that, you are not using the correct types.argcisintegerandargvisarray of strings(which arearrays of chars). Soargvis an array of arrays ofchars.Then, simply use the argc counter to loop through the argv array.
argv[0]is the name of the program, andargv[1]toargv[n]will be the arguments you pass to your program while executing it.Here is a good explanation on how this works: http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basics/#command-line
My 2 cents.
EDIT: Here is a commented version of the working program.
I hope it helps.