0

I have a C application that does this features:

  • being called by an Internet protocol (syntax: gett:test!test123!456 ).
  • reads a file and parse it (the file will be exactly on the same folder as the exe and name is defined.
  • parses the syntax I wrote previously (takes test!test123!456 as argv) and parse it
  • calls another application with the parsed arguments (from above)

I have successfuly make the features. Otherwise, the only problem (currently) is: when I call the application from another location or application (e.g cmd), the application seems to be losing the file defined above. the exception handler for this, in my code is:

    /// open the config.txt module
FILE *f;
f=fopen("configgg.txt", "r");

if (f == 0) { /* If opening failed */
    MessageBox(NULL, "Failed in opening file. Make sure you have the config.txt", "Error", MB_OK | MB_ICONINFORMATION);
    return -1;
}

Can anyone help me with this problem?

Sorry for the broken language. Thank you in advance, I appreciate any sort of help.

Winz
  • 11
  • 1
  • 2
    `fopen("configgg.txt", "r")` is looking for the file in whatever folder you are executing from. You'll need to include the path to the file. – lurker May 05 '14 at 13:21
  • That's it. the file is supposed to be on the same folder as the application is. but, I don't understand how do I fix the definition. also, this application may be placed in different folders. that leads to the variable location, not 100% path static. – Winz May 05 '14 at 13:24
  • If the config file is always with the executable, then you'll need to programmatically [get the path of the executable](http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe/1024937#1024937), then use that same path for the config file. Just to test things out, you could hard-code the paths, but in the long run, you probably want to do it programmatically to avoid issues later. – lurker May 05 '14 at 13:26
  • You should check `argv[0]`, it should have the path to your executable, just trim what come after the last `/` or `\\`, and add your file name. – DrakaSAN May 05 '14 at 13:30
  • Okay, I have checked `argv[0]` and I managed to have `GetModuleFileName` working. both gives me a directory and the exe name. now, how should I trim it? (currently, I parse, cut, and store everything into char arrays (using `strtok`). later on, I use `strcat` to combine them. is there any better solution for this? thanks – Winz May 05 '14 at 13:41
  • I don t think so. String manipulation in C are limited... – DrakaSAN May 05 '14 at 13:50
  • okay, so I will be using the method I am using. I'll try. – Winz May 05 '14 at 15:49
  • 1
    At stack-overflow, accept an answer to show the problem is solved. Do not change the title to "(solved)". Please undo the edit and accept an answer (posting your own if needed.) – chux - Reinstate Monica May 05 '14 at 18:48

2 Answers2

0

As is, you call a relative path to the path the user is in. You should make the path relative to your application.

You can get the full path to your application via argv[0], you can then trim the end and concatene to the name of your config file to get the full path of your config file.

DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
0

Okay, I have finally solved this problem. Solution:

TCHAR buffer[MAX_PATH];
GetModuleFileName(NULL,buffer,sizeof(buffer));
char *s = strtok(buffer, ":\\");
char *d[250];
int i=0;
int j=0;

while(s!=NULL){
    d[i]=s;
    s = strtok(NULL, ":\\");
    i++;
    j++;
}

for (i=0; i<j; i++){
    printf("d[%d]: %s\n", i, d[i]);
}
j--;
char theloc[250]="";

for (i=0; i<j; i++){
    strcat(theloc,d[i]);
    if (i==0) {
        strcat(theloc, ":\\");
    } else if (i==j-1){
        strcat(theloc, "\\");
    } else {
    strcat(theloc, "\\");
    }
}

Thank you very much for the help, @lurker @DrakaSAN (as well as the minus vote. I know this question is stupid yet worthless)

Winz
  • 11
  • 1