Question

I'm having a hard time trying to pass a directory path to my program on NetBeans 7.2, what I tried to do was to write "${OUTPUT_PATH}" "/home/vitor/Área de Trabalho/Programação/Teste" on the project's parameters. /home/vitor/Área de Trabalho/Programação/Teste is my directory's path, I have 3 .txt files inside it, and my program is supposed to read each one of them by adding their names in the path's ending, something like:

/home/vitor/Área de Trabalho/Programação/Teste/times.txt

Here is my piece of code:

int main(int argc, char *argv[]){
if(argc == 1){
    printf("ERROR: The directory's path wasn't informed.");
    exit(1);
}
else{
    char endtimes[200];
    strcpy(endtimes, argv[1]);
    strcat(endtimes, "times.txt");
    }    
FILE *caminho;
caminho = fopen(endtimes, "r");
if (!caminho){
    printf("Error trying to open file.");
    exit(1);
}

Everytime I try to run the code, it displays Error trying to open file. I checked argc and it's value is 4 (which I guess is not right.) I don't have enough experience using netbeans, in fact, this is my first program to work with files. So, could you guys help me?

I'm using Ubuntu 13.

Thanks for your patience.

--EDIT--

I made changes on the project's parameters according to the comments below, endtimes is storing the correct file path : /home/vitor/Área de Trabalho/Programação/Teste/times.txt but I still get Error trying to open file. Should the file path be different because I'm using Ubuntu 13?

Was it helpful?

Solution

argv[0] is the executable's name and if you've passed 3 arguments in the form of filenames, those will be stored in argv[1], argv[2] and argv[3], respectively. So you might want something like:

strcpy(endjogos, argv[1]);
strcat(endjogos, "jogos.txt");
strcpy(endtimes, argv[2]);
strcat(endtimes, "times.txt");
strcpy(endapost, argv[3]);
strcat(endapost, "apostas.txt");

Note that C arrays are zero-indexed, so with argc==4, you have argv[0], argv[1], argv[2], argv[3].

OTHER TIPS

If you have a program call like this "main.out -one.txt -two.txt tree.txt" you have 4 parameters. Thereforeargc will be 4 argv[0] will be "main.out" argv[1] will be "one.txt" argv[2] will be "two.txt" argv[3] will be "tree.txt"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top