如何将缓冲区中的路径用作fprintf中的文件路径 [英] How to use path in a buffer as file path in fprintf
本文介绍了如何将缓冲区中的路径用作fprintf中的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的代码:-
i had the code like this:-
void Log ::CreateLogFile1()
{
char szDate[12];
char buf[150];
int i=0;
int iLevel;
//get the path of ini file
// TO DO
//check status of ini file
// TO DO
//read ini file and get log directory path
// TO DO
//create log file
_strdate( szDate );
//logfilename format ServerManagermm-dd--yy
strcpy(buf,szLogDir1);
strcat(buf, "\\");
strcat(buf, "\\");
strcat(buf,a);
strcat(buf,szDate);
strcat(buf,".txt");
while(buf[i] != '\0') // mm/dd/yy to mm-dd-yy conversion
{
if(buf[i] == '/')
buf[i] = '-';
i++;
}
ofstream pOS(buf,ios::app);
pOS<<"\nBinary Strarted..\n"<<endl;
pOS.close();
}
我这样修改它:-
i modified it as like this:-
void Log ::WriteBinary(char bufg[139])
{
char szDate[12];
char szTime[12];
char buf[2000];
char bufpath[2000];
int i=0;
_strdate( szDate );
_strtime( szTime );
strcpy(buf,szLogDir1);
strcat(buf, "\\");
strcat(buf, "\\");
strcat(buf,a);
strcat(buf,szDate);
strcat(buf,".txt");//as buf contain my file path and i want to use it as FILE*
memcpy(bufpath,buf,sizeof(buf));
while(buf[i] != '\0')
{
if(buf[i] == '/')
buf[i] = '-';
i++;
}
ofstream pOS(buf,ios::app);
fprintf((FILE*)bufpath, "%s::%s::DATA IS::", szDate, szTime);
fwrite(bufg, 1, 138, (FILE*)bufpath);
fprintf((FILE*)bufpath, "\n");
pOS<<buf<< endl;
pOS.close();
}
我的代码在以下行中中断:"
my code break at the following line "
fprintf((FILE*)bufpath, "%s::%s::DATA IS::", szDate, szTime);
"
有人可以告诉吗?
"
Can anyone tell on this?
推荐答案
fprintf
期望有效的FILE *
.
那就是您必须首先打开文件(请参见打开 [
fprintf
expects a validFILE *
.
That is you must first open the file (see fopen[^]) to obtain a validFILE
pointer and then pass such file pointer tofprintf
. e.g.
FILE * fp = fopen(bufpath, "r");
if (fp)
{
fprintf(fp, "Hi");
}
else
{
// handle file open error
}
正如超人所指出的,您的演员表是邪恶的.
As already noted by Superman your cast is evil.
fprintf
用于文件.
对于缓冲区,请使用sprintf
.
记住,类型转换是邪恶的.
fprintf
is used for files.
For buffers usesprintf
.
And remember, typecasting is evil.
这篇关于如何将缓冲区中的路径用作fprintf中的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文