返回信息流进行文件操作,先是打开,但是每次建立的文件名字只是输入的一串字符中的第一个,代码如下:
//test.c
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwRet;
errno_t err;
FILE *pfHandle;
if ( NULL != argv[1] )
{
/* Open FILE with name argv[1] in Mode a+ ; For first
time Create a new FILE */
if( (err = fopen_s( &pfHandle, (CHAR *)argv[1],
"a+" )) !=0 )
{
printf( "The file %s was not opened\n",argv[1]
);
}
else
{
printf( "The file %s was opened\n",argv[1] );
}
}
//TODO:Things i wanted operation on FILE
// Close FILE pfHandle if it is not NULL
if( NULL != pfHandle)
{
if ( fclose( pfHandle ) )
{
printf( "The file %s was not closed\n",argv[1]
);
}
}
return 0;
}
执行时:
>test.exe myfile.txt
回车执行,新建的文件名为m
哪位高手见过,给我指导一下。。万分感谢。
这是一条镜像帖。来源:北邮人论坛 / soft-design / #25443同步于 2008/5/16
该镜像源已超过 30 天没有更新,可能在源站已被删除。
SoftDesign机器人发帖
请教:C进行文件操作,为什么文件名一直只是一个字符?
strongpan
2008/5/16镜像同步19 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
【 在 strongpan 的大作中提到: 】
: 进行文件操作,先是打开,但是每次建立的文件名字只是输入的一串字符中的第一个,代码如下:
: //test.c
: int _tmain(int argc, _TCHAR* argv[])
: ...................
if( (err = fopen_s( &pfHandle, (CHAR *)argv[1],
"a+" )) !=0 )
【 在 he1l0 的大作中提到: 】
: if( (err = fopen_s( &pfHandle, (CHAR *)argv[1],
:
: "a+" )) !=0 )
我的代码中也有这个&操作啊。。??
fopen_s 与fopen关系在MSDN中的解释是:
these are versions of fopen with security enhancements.
函数原型为:
errno_t fopen_s(
FILE** pFile,
const char *filename,
const char *mode
);
对该问题,相关部分采用下面的方法却可以生成完全的文件名,你帮忙参考下:
if( (err = fopen_s( &pfHandle, (CHAR *)“test.txt”, "a+" )) !=0 )
{
printf( "The file %s was not opened\n",argv[1] );
}
else
{
printf( "The file %s was opened\n",argv[1] );
}
即不使用main函数的入参,而直接在代码中写入文件名,这也运行代码就可以生成test.txt的文件了。
#ifdef UNICODE
typedef wchar_t TCHAR;
#else
typedef unsigned char TCHAR;
#endif
typedef unsigned char CHAR;
typedef unsigned wchar_t WCHAR;
恩...
【 在 strongpan (sp) 的大作中提到: 】
: 呵呵,我也看着别扭,
: 不过你看MSDN中对_tmain()函数的定义就是这样的。
: 不知道_TCHAR是个什么东西。。
: ...................
【 在 CNLAS 的大作中提到: 】
: #ifdef UNICODE
: typedef wchar_t TCHAR;
: #else
: ...................
re
windows data type