-1

I find that the API,

_tfsopen()

is not working when I just give the relative file path (JUST FILE NAME) in

fopen_s(&fp,"FileName.txt",w+);

But if I give the absolute path of the txt file then it's working fine. So the problem is in getting the current working directory (CWD). How do I get the CWD and make this API work?

This API definition is present in:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\crt\src\fopen.c

This is what fopen.c has:

errno_t __cdecl _tfopen_s (
        FILE ** pfile,
        const _TSCHAR *file,
        const _TSCHAR *mode
        )
{
    _VALIDATE_RETURN_ERRCODE((pfile != NULL), EINVAL);
    *pfile = _tfsopen(file, mode, _SH_SECURE);  // ERROR IS OCCURING AT THIS LINE. IT's NOT ABLE TO OPEN THE FILE.

    if(*pfile != NULL)
        return 0;

    return errno;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
codeLover
  • 3,720
  • 10
  • 65
  • 121
  • where are you trying to write to "FileName.txt" ? is it in a place that requires admin approval? Also, have you tried putting the actual location is too? or maybe "./FileName.txt" ? – Rhexis May 31 '12 at 13:58
  • 1
    What makes you think `_tfopen_s` is part of any API? It looks like an internal function to me, not something you're supposed to invoke yourself. That's why its name begins with an underscore and why you've found it in a `.c` file, not a `.h` file. – Lightness Races in Orbit Feb 17 '15 at 23:27
  • possible duplicate of [How to get Current Directory?](http://stackoverflow.com/questions/875249/how-to-get-current-directory) – sashoalm Feb 18 '15 at 09:35
  • @LightnessRacesinOrbit It's a documented Microsoft-specific function (macro, really): https://msdn.microsoft.com/en-us/library/z5hh6ee9.aspx - the very example of which actually uses relative file names, showing that this should work, and the OP's problem must be somewhere else. – Sebastian Redl Feb 18 '15 at 09:41

1 Answers1

0

This code let you get current working directory:

#include <direct.h>

char cwd[MAX_PATH_SIZE];
_getcwd(cwd, MAX_PATH_SIZE);
Yola
  • 18,496
  • 11
  • 65
  • 106