1. argc is the number or arguments on the command line, including the name of the program. So, if you type "./a.out filename.c 234 323 <Enter key>" the value of argc will be 4. The test for argc in your program will be wrong. Instead of testing for 2 it should test for > 1
Code:
if( argc > 1)
filename = argv[1];
2. The same file can not normally be opened twice -- if the first fopen() succeeds then the second is probably going to fail. If you want the file for both reading and writing then open it once and specify "r+", "w+", or "a+", depending on how you want it opened. see man pages or msdn for description of these flags.
Code:
#include <stdio.h> //Allows input and output of program
#include <io.h> //Checks for existence of file
int main(int argc, char* argv[])
{
FILE* fp;
int i;
if( argc < 2)
{
printf("Error\n");
return 1;
}
if( (fp = fopen(argv[1],"w")) == NULL)
{
printf("can't open file %s\n", argv[1]);
return 1;
}
// write out each of the remaining arguments
for(i = 2; i < argc; ++i)
fprintf(fp, "%s\n", argv[i]);
fclose(fp);
return 0;
}