0

I am running an exe that reads text from a file, which is saved in the same directory as the exe. I am using the _tfopen function, that returns errno 2, which means that the file is missing. As is already said, the text file is in the same directory as the exe, so Im using only filename of the text file and not the whole path (even though ive tried to use the absolute filepath...). I am running the exe from a different computer, not the one I am developing on (The release version).

Another application that using the same file, also in the same directory, works perfectly.

Why does the exe cant find the text file?

FILE* fileData;
if ((fileData = _tfopen(_T("Data.txt"), _T("r"))) == NULL)
    return false;
g-los
  • 33
  • 1
  • 7
  • It doesn't matter where exe is placed. What is current directory? – Alex F Mar 05 '13 at 12:58
  • In the VS2010 it is set to be the project directory, but i run this exe on a different computer, and the text file is in the same directory. – g-los Mar 05 '13 at 13:02
  • hope your program does not call chdir, which changes working directory of process. As well instead of giving exe full path, Can you give full path of file to be opened, It will clear whether your program calls chdir or not. – rahul.deshmukhpatil Mar 05 '13 at 13:04

3 Answers3

2

You might want to try this: have your program call GetCurrentDirectory() and output the result somewhere to that you can confirm that the program's current directory is truly what you think it is. You could use OutputDebugString() if you are running under a debugger or MessageBox() if not.

When I need to be sure I'm opening a co-located file, I call GetModuleFileName( NULL, szExeFqn, sizeof(szExeFqn)) to get the FQN of the EXE file, strip off the EXE file name and add on the name of the file I need. That way, I'm not making any assumptions that might be false -- after all, it is trivial to have a shortcut set the default directory for a program different from the directory containing the EXE file.

Also, if you run your program from a command line by entering the full path to your program, then the program's current directory is the one you were in when you ran it, not the one where the EXE was found. That's also true if your program is found by searching the PATH environment variable (yes, it still exists in Windows.)

Here's an example of what I've used for more than a decade to do what you've described:

char szHelpFileName[_MAX_FNAME];
char *cp;

GetModuleFileName( NULL, szHelpFileName, sizeof(szHelpFileName) );
cp = strrchr( szHelpFileName, '\\' );
if( cp )
{
   cp++; // Point to the char just following the final slash.
}
else
{  // No backslash, is there a colon?
   cp = strrchr( szHelpFileName, ':' );
   if( cp )
   {
      cp++; // Point to the char just following the colon.
   }
   else
   {  // I give up.  I'll have no path information.
      cp = szHelpFileName;
   }
}
strcpy( cp, "program.hlp" );

The final result is the name of a help file that is co-located with the EXE. Unless the help file is missing (or its permissions have been scrogged somehow) this always works. I've never had it follow the "paranoia" path where the comment says "I give up."

Steve Valliere
  • 1,119
  • 7
  • 12
  • When the file exists, and your process/user has the necessary rights, this works 100% of the time. If you know those facts are true, then please post code so we might help you find the mistake in it. – Steve Valliere Mar 05 '13 at 13:25
1

Usually operating systems open files with relative address based on the current directory, rather than the executable location.

So for example if your executable is in /path/to/exec/executable and you invoke it from /path/to/invoke, it will try to open Data.txt as if it was /path/to/invoke/Data.txt.

You can take a look at this question and its answers to find out how to find the path to executable in Windows. Or this answer for various other operating systems.

Community
  • 1
  • 1
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
0

It should work even without specifying the full path. Do you launch the executable from Visual Studio? If yes, make sure that the Data.txt file is inside the project directory where the *.vcxproj file or the *.sln file is.

feradz
  • 1,312
  • 1
  • 16
  • 29