0

I am working on an application in C, using Visual Studio 2015 for getting the files from a path.

Idea is that I run the application and I write the command: list path.

The application should go at that path and list me all the files from there. All is fine, but I need to save the files in a text file which should be in the project folder.

My problem is that I can't go back from the path I write in command to project path.

For example, I have project in C:\\Visual Studio\\projects\\application, and I want to create the text file here, but when I write from my keyboard in console that path, for example: list C:\\MinGWStudio, the text file will be create there.

The code:

printf("Comanda a fost executata cu succes!\n");
SetCurrentDirectory(argv[0]);
FILE *outFile;
outFile = fopen("list.txt", "w+");
printf("%s\n", lista->cale);
fprintf(outFile, "%s\n", lista->cale);
Mathieu
  • 8,840
  • 7
  • 32
  • 45
Sebi95
  • 161
  • 4
  • 13
  • 1
    Please check `argv[0]`, it's not what you apparently think it is. Always check the return value of `fopen()`, you might not have permissions in the target directory and it would fail. – Iharob Al Asimi May 12 '16 at 19:46
  • Possible duplicate of [What does char \* argv\[\] means?](http://stackoverflow.com/questions/16666353/what-does-char-argv-means) – Mad Physicist Jun 11 '16 at 14:10

1 Answers1

0

Apparently you want

SetCurrentDirectory(argv[1]);
/*                       ^ 1 not 0 */

argv[0] is the name of the current program, so it's not a directory. Also, I don't know if or what SetCurrentDirectory() returns, but if I was the API designer, surely I would make it return something that would inform the caller about failure or success.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97