0

in codeeval, some challenges ask us to test cases are read in from a file which is the first argument to your program, what is that mean, the first argument to your program?

my code can work in my pc, but when I submit the solution, it show I am wrong, I don't know how to change the path to the first argument.

my code as follows:

#include <stdio.h>
#include <stdlib.h>
int main()
{

FILE *fp;
int num[100];
int i;
int sum = 0;
char ch;
int reminder;

fp = fopen("one.txt", "r+");

do{
    fscanf(fp, "%d", &num[i]);
    while(num[i]!=0){
        reminder = num[i]%10;
        sum = sum + reminder;
        num[i] = num[i]/10;
    }
    printf("%d\n", sum);
    sum = 0;
    i++;
}while((ch = fgetc(fp)) != EOF);

fclose(fp);

system("pause");
return 0;
}

1 Answers1

0

When you start the program, in a Unix shell or a Windows command window, you can give an argument to it. If your program is called, for example, "runtests", you would type "runtests file.txt" on a line, and press ENTER. "file.txt" is an argument (the first and only argument) to your program.

To let your program access these arguments, you need to add parameters to main:

int main(int argc, char *argv[]) {
    // ....
    fp = fopen(argv[1], "r+");
    // ....

Note that argc will be 2, since argv[0] will be the name of the program itself, "runtests".

I believe you can also drag and drop a file icon onto the icon for your executable file, and the first argument will then be the full path name of the dragged-and-dropped file.

Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75