当前位置: 技术问答>linux和unix
Linux C下fdopen的问题
来源: 互联网 发布时间:2016-10-12
本文导语: void LogManage::GetIndex(void) { if(!m_bOpen) { ReadIndex(); } printf("Current log file: [%s] n", m_ppFileList[m_nIndex]); } void LogManage::Clean() { Close(); for(int i = 0; i = 50) { WriteIndex(); } fputc('n', m_fStream); if(m_nLineCount >= MAX_LINE_COU...
void LogManage::GetIndex(void)
{
if(!m_bOpen)
{
ReadIndex();
}
printf("Current log file: [%s] n", m_ppFileList[m_nIndex]);
}
void LogManage::Clean()
{
Close();
for(int i = 0; i = 50)
{
WriteIndex();
}
fputc('n', m_fStream);
if(m_nLineCount >= MAX_LINE_COUNT)
{
close(m_fdLog);
// fclose(m_fStream);
m_nIndex = (m_nIndex + 1) % FILE_COUNT;
m_fdLog = open(m_ppFileList[m_nIndex], O_CREAT | O_TRUNC | O_WRONLY, 0666);
if(-1 == m_fdLog)
{
printf("open file failed! Line: %dn", __LINE__);
}
m_nLineCount = 0;
/* why NOT work??????
* m_fStream = fdopen(m_fdLog, "a+"); // "a+" means append
if(NULL == m_fStream)
{
printf("fdopen failed! Line: %dn", __LINE__);
}
*/
/* why NOT work?????
char pBuf[100];
sprintf(pBuf, "nnn******************************* %d *****************nnn", m_nLogTimer); // Indicate the file sequence
fputs(pBuf, m_fStream);
fflush(m_fStream);
*/ fprintf(m_fStream, "nnn******************************* %d *****************nnn", m_nLogTimer); // Indicate the file sequence
fflush(m_fStream);
WriteIndex();
}
fflush(m_fStream);
fdatasync(m_fdLog);
}
bool LogManage::WriteIndex(void)
{
m_fdIndex = open(m_pIndexFile, O_RDWR);
if(-1 == m_fdIndex) // open fail, maybe haven't been created
{
printf("Index file %s dose NOT exists! n", m_pIndexFile);
return false;
}
else // open successfully
{
bool bRet = true;
lseek(m_fdIndex, 0, SEEK_SET);
bRet = bRet && (-1 != write(m_fdIndex, &m_nIndex, sizeof(m_nIndex))); // write m_nIndex
bRet = bRet && (-1 != write(m_fdIndex, &m_nLineCount, sizeof(m_nLineCount))); // write m_nLineCount
bRet = bRet && (-1 != write(m_fdIndex, &m_nLogTimer, sizeof(m_nLogTimer))); // write m_nLogTimer
fdatasync(m_fdIndex);
bRet = bRet && (-1 != close(m_fdIndex)); // close index file
return bRet;
}
}
bool LogManage::ReadIndex(void)
{
bool bRet = true;
m_fdIndex = open(m_pIndexFile, O_RDWR);
if(-1 == m_fdIndex) // open fail, maybe haven't been created
{
printf("Index file %s dose NOT exists! Create it!n", m_pIndexFile);
m_fdIndex = open(m_pIndexFile, O_CREAT | O_RDWR, 0666);
if(-1 == m_fdIndex)
{
error("Create index file %s FAILED! Errno: %d", m_pIndexFile, errno);
bRet = false;
}
else
{
int nTemp = 0;
bRet = bRet && (-1 != write(m_fdIndex, &nTemp, sizeof(m_nIndex)));
bRet = bRet && (-1 != write(m_fdIndex, &nTemp, sizeof(m_nLineCount)));
bRet = bRet && (-1 != write(m_fdIndex, &nTemp, sizeof(m_nLogTimer)));
fdatasync(m_fdIndex);
m_nIndex = 0;
m_nLineCount = 0;
m_nLogTimer = 0;
}
}
else // open successfully
{
lseek(m_fdIndex, 0, SEEK_SET);
if(read(m_fdIndex, &m_nIndex, sizeof(m_nIndex))
{
if(!m_bOpen)
{
ReadIndex();
}
printf("Current log file: [%s] n", m_ppFileList[m_nIndex]);
}
void LogManage::Clean()
{
Close();
for(int i = 0; i = 50)
{
WriteIndex();
}
fputc('n', m_fStream);
if(m_nLineCount >= MAX_LINE_COUNT)
{
close(m_fdLog);
// fclose(m_fStream);
m_nIndex = (m_nIndex + 1) % FILE_COUNT;
m_fdLog = open(m_ppFileList[m_nIndex], O_CREAT | O_TRUNC | O_WRONLY, 0666);
if(-1 == m_fdLog)
{
printf("open file failed! Line: %dn", __LINE__);
}
m_nLineCount = 0;
/* why NOT work??????
* m_fStream = fdopen(m_fdLog, "a+"); // "a+" means append
if(NULL == m_fStream)
{
printf("fdopen failed! Line: %dn", __LINE__);
}
*/
/* why NOT work?????
char pBuf[100];
sprintf(pBuf, "nnn******************************* %d *****************nnn", m_nLogTimer); // Indicate the file sequence
fputs(pBuf, m_fStream);
fflush(m_fStream);
*/ fprintf(m_fStream, "nnn******************************* %d *****************nnn", m_nLogTimer); // Indicate the file sequence
fflush(m_fStream);
WriteIndex();
}
fflush(m_fStream);
fdatasync(m_fdLog);
}
bool LogManage::WriteIndex(void)
{
m_fdIndex = open(m_pIndexFile, O_RDWR);
if(-1 == m_fdIndex) // open fail, maybe haven't been created
{
printf("Index file %s dose NOT exists! n", m_pIndexFile);
return false;
}
else // open successfully
{
bool bRet = true;
lseek(m_fdIndex, 0, SEEK_SET);
bRet = bRet && (-1 != write(m_fdIndex, &m_nIndex, sizeof(m_nIndex))); // write m_nIndex
bRet = bRet && (-1 != write(m_fdIndex, &m_nLineCount, sizeof(m_nLineCount))); // write m_nLineCount
bRet = bRet && (-1 != write(m_fdIndex, &m_nLogTimer, sizeof(m_nLogTimer))); // write m_nLogTimer
fdatasync(m_fdIndex);
bRet = bRet && (-1 != close(m_fdIndex)); // close index file
return bRet;
}
}
bool LogManage::ReadIndex(void)
{
bool bRet = true;
m_fdIndex = open(m_pIndexFile, O_RDWR);
if(-1 == m_fdIndex) // open fail, maybe haven't been created
{
printf("Index file %s dose NOT exists! Create it!n", m_pIndexFile);
m_fdIndex = open(m_pIndexFile, O_CREAT | O_RDWR, 0666);
if(-1 == m_fdIndex)
{
error("Create index file %s FAILED! Errno: %d", m_pIndexFile, errno);
bRet = false;
}
else
{
int nTemp = 0;
bRet = bRet && (-1 != write(m_fdIndex, &nTemp, sizeof(m_nIndex)));
bRet = bRet && (-1 != write(m_fdIndex, &nTemp, sizeof(m_nLineCount)));
bRet = bRet && (-1 != write(m_fdIndex, &nTemp, sizeof(m_nLogTimer)));
fdatasync(m_fdIndex);
m_nIndex = 0;
m_nLineCount = 0;
m_nLogTimer = 0;
}
}
else // open successfully
{
lseek(m_fdIndex, 0, SEEK_SET);
if(read(m_fdIndex, &m_nIndex, sizeof(m_nIndex))