如何将此PHP函数转换为C?
function adx_store_data(filepath, data)
{
$fp = fopen(filepath,"ab+");
if($fp)
{
fputs($fp,data);
fclose($fp);
}
}
如何将此PHP函数转换为C?
function adx_store_data(filepath, data)
{
$fp = fopen(filepath,"ab+");
if($fp)
{
fputs($fp,data);
fclose($fp);
}
}
#include <stdio.h>
void adx_store_data(const char *filepath, const char *data)
{
FILE *fp = fopen(filepath, "ab");
if (fp != NULL)
{
fputs(data, fp);
fclose(fp);
}
}
#include <stdio.h>
: : :
int adxStoreData (char *filepath, char *data) {
int rc = 0;
FILE *fOut = fopen (filepath, "ab+");
if (fOut != NULL) {
if (fputs (data, fOut) != EOF) {
rc = 1;
}
fclose (fOut); // or for the paranoid: if (fclose (fOut) == EOF) rc = 0;
}
return rc;
}
它检查各种错误条件,例如文件I/O问题,并在一切正常时返回1(true),否则返回0(false)。即使在PHP中,这可能是您应该做的事情。
fclose()
是否成功不仅是多虑。输出通常是缓冲的;直到fclose()
调用才有可能实际写入文件。 - Keith Thompson