首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

fopen("abc.txt" "w"); 文件建在哪儿的有关问题

2013-06-25 
fopen(abc.txt, w) 文件建在哪儿的问题FILE* logfile fopen(abc.txt, w)fclose(logfile)之前

fopen("abc.txt", "w"); 文件建在哪儿的问题

FILE* logfile = fopen("abc.txt", "w");
fclose(logfile);

之前一直这样用Windows上、linux上,abc.txt都是建在生成的可执行程序文件夹(dev-c是这样.VS生成到项目所在文件夹,但也差不多).

因为C标准里没有写这个函数不写路径的时候建在哪,所以我一直觉得是操作系统决定的.

后来我用codeblocks,这样写就建在了C盘根目录下.

我想知道,像这种函数,最终决定生成文件建在哪,是谁决定的?是编译器?还是编译器选项?,比如编译器可以设置一些环境变量什么的.
[解决办法]
默认为当前目录,即程序所在的目录。
[解决办法]
后来我用codeblocks,这样写就建在了C盘根目录下
----------------------
编译后的exe文件单独运行呢?
我觉得肯定是当前文件夹吧。。。
[解决办法]
一般建立在可执行文件所在目录
[解决办法]
程序所在的当前目录
[解决办法]
创建在当前的目录,就是调用你那个程序的那个目录。不然你在C:\执行那个程序就在C:\创建
你在那个程序目录执行,就在那个可执行程序目录。
[解决办法]
程序都有当前路径的概念
[解决办法]
和执行程序在同一个目录。
[解决办法]
当前工作目录(currrent working directoy),不是程序所在的目录.

edit.exe myfile.txt 

myfile.txt会在你执行以上命令的目录下,而不是edit.exe所在的目录.
[解决办法]
_getcwd, _wgetcwd
Get the current working directory.

char *_getcwd( char *buffer, int maxlen );

wchar_t *_wgetcwd( wchar_t *buffer, int maxlen );

Routine Required Header Compatibility 
_getcwd <direct.h> Win 95, Win NT 
_wgetcwd <direct.h> or <wchar.h> Win NT 


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version 
LIBCMT.LIB Multithread static library, retail version 
MSVCRT.LIB Import library for MSVCRT.DLL, retail version 


Return Value

Each of these functions returns a pointer to buffer. A NULL return value indicates an error, and errno is set either to ENOMEM, indicating that there is insufficient memory to allocate maxlen bytes (when a NULL argument is given as buffer), or to ERANGE, indicating that the path is longer than maxlen characters.

Parameters

buffer

Storage location for path

maxlen

Maximum length of path in characters: char for _getcwd and wchar_t for _wgetcwd

Remarks



The _getcwd function gets the full path of the current working directory for the default drive and stores it at buffer. The integer argument maxlen specifies the maximum length for the path. An error occurs if the length of the path (including the terminating null character) exceeds maxlen. The buffer argument can be NULL; a buffer of at least size maxlen (more only if necessary) will automatically be allocated, using malloc, to store the path. This buffer can later be freed by calling free and passing it the _getcwd return value (a pointer to the allocated buffer).

_getcwd returns a string that represents the path of the current working directory. If the current working directory is the root, the string ends with a backslash ( \ ). If the current working directory is a directory other than the root, the string ends with the directory name and not with a backslash.

_wgetcwd is a wide-character version of _getcwd; the buffer argument and return value of _wgetcwd are wide-character strings. _wgetcwd and _getcwd behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined 
_tgetcwd _getcwd _getcwd _wgetcwd 


Example

// GETCWD.C
/* This program places the name of the current directory in the 
 * buffer array, then displays the name of the current directory 
 * on the screen. Specifying a length of _MAX_PATH leaves room 
 * for the longest legal path name.
 */

#include <direct.h>
#include <stdlib.h>
#include <stdio.h>

void main( void )
{
   char buffer[_MAX_PATH];

   /* Get the current working directory: */
   if( _getcwd( buffer, _MAX_PATH ) == NULL )
      perror( "_getcwd error" );
   else
      printf( "%s\n", buffer );
}


Output

C:\code


Directory Control Routines

See Also   _chdir, _mkdir, _rmdir

GetCurrentDirectory
The GetCurrentDirectory function retrieves the current directory for the current process. 

DWORD GetCurrentDirectory(
  DWORD nBufferLength,  // size, in characters, of directory buffer
  LPTSTR lpBuffer       // pointer to buffer for current directory
);


 
Parameters
nBufferLength 
Specifies the length, in characters, of the buffer for the current directory string. The buffer length must include room for a terminating null character. 
lpBuffer 
Pointer to the buffer for the current directory string. This null-terminated string specifies the absolute path to the current directory. 
Return Values
If the function succeeds, the return value specifies the number of characters written to the buffer, not including the terminating null character. 

If the function fails, the return value is zero. To get extended error information, call GetLastError. 

If the buffer pointed to by lpBuffer is not large enough, the return value specifies the required size of the buffer, including the number of bytes necessary for a terminating null character. 

QuickInfo
  Windows NT: Requires version 3.1 or later.
  Windows: Requires Windows 95 or later.
  Windows CE: Unsupported.
  Header: Declared in winbase.h.
  Import Library: Use kernel32.lib.
  Unicode: Implemented as Unicode and ANSI versions on Windows NT.

See Also
File I/O Overview, File Functions, CreateDirectory,GetSystemDirectory,GetWindowsDirectory, RemoveDirectory, SetCurrentDirectory 

 

热点排行