如何于程序所在目录创建文件?
admin 发表于 2010-04-21 | 来源:互联网 | 阅读:
我需要编写一个程序,该程序会创建一个文件,但默认的fopen_s创建的文件是在
C:\Users\MyName\
怎么才能使文件创建到程序所在目录呢?
我需要编写一个程序,该程序会创建一个文件,但默认的fopen_s创建的文件是在
C:\Users\MyName\
怎么才能使文件创建到程序所在目录呢?
评论功能因故关闭!
请加入我们的QQ群一起参与讨论:群号59400482(500人超级群)
".\\myfile"
GetCurrentDirectoryThe 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);
GetCurrentDirectoryThe GetCurrentDirectory function retrieves the current directory for the current process. DWORD GetCurrentDirectory( DWORD nBufferLength, // size, in characters, of directo……程序所在目录,不是用 GetModuleFileName吗?
创建文件是你自己把路径给出来就行了撒,如:D:\file\file.dat
不知道楼主有何用意?
程序所在的目录,就是直接"./"啊,或是直接空路径
如果之前修改过当前目录,./可能就不灵了。GetModuleFileName是取指定模块所在目录
我不想用GetModuleFileName,我还想把程序移植到linux呢.我试过 帮助文档 // crt_fopen_s.c// This program opens two files. It uses// fclose to close the first file and// _fcloseall to close all remaining files. #include <stdio.h>FILE *stream, *stream2;int main( void ){ int numclosed; errno_t err; // Open for read (will fail if file "crt_fopen_s.c" does not exist) if( (err = fopen_s( &stream, "crt_fopen_s.c", "r" )) !=0 ) printf( "The file ‘crt_fopen_s.c’ was not opened\n" ); else printf( "The file ‘crt_fopen_s.c’ was opened\n" ); // Open for write if( (err = fopen_s( &stream2, "data2", "w+" )) != 0 ) printf( "The file ‘data2′ was not opened\n" ); else printf( "The file ‘data2′ was opened\n" ); // Close stream if it is not NULL if( stream) { if ( fclose( stream ) ) { printf( "The file ‘crt_fopen_s.c’ was not closed\n" ); } } // All other files are closed: numclosed = _fcloseall( ); printf( "Number of files closed by _fcloseall: %u\n", numclosed );}能行,但是用到我的程序上也没有改多少 char filename[20], tmp_char; FILE *fp; scanf_s("%s",filename,20); errno_t err; //打开文件只读 if( (err = fopen_s( &fp,filename, "r" )) != 0 ) { printf(">文件%s没有找到.\n",filename); system("pause"); exit(0); } else { tmp_char=fgetc(fp); putchar(tmp_char); printf( ">文件%s已经读取.\n",filename ); system("pause"); fclose(fp); }就提示的是,没有输入错误.文件google.txt没有找到.我确实是在程序所在目录有个google.txt.为了以防万一在主文件夹下也放了个google.txt.也报同样的错.
帮顶。
顶帖~~~