我有这个方法,它应该获取缓冲区并将一些内容写入文件:
void writeTasksToDevice()
{
TaskInfo *task;
unsigned int i = lastTaskWritten;
printf("writing elihsa\n");
outputFile.write(" Elisha2", 7);
//pthread_mutex_lock(&fileMutex);
for(; i < (*writingTasks).size(); i++)
{
task = (*writingTasks).at(i);
if(NULL == task)
{
printf("ERROR!!! in writeTasksToDevice - there's a null task in taskQueue. By "
" design that should NEVER happen\n");
exit(-1);
}
if(true == task->wasItWritten)
{
//continue;
}
else // we've found a task to write!
{
printf("trying to write buffer to file\n");
printf("buffer = %s, length = %d\n", task->buffer, task->length);<====PRINT HERE IS OK< PRINTING WHAT IS WANTED
outputFile.write(task->buffer, task->length); <===SHOULD WRITE HERE
printf("done writing file\n");
}
}
//pthread_mutex_unlock(&fileMutex);
// TODO: check if we should go to sleep and wait for new tasks
// and then go to sleep
}
缓冲区内容为:
任务 - &gt;缓冲区:elishaefla
任务 - &gt;长度:10
我使用以下方法在另一个init函数中打开了流:
outputFile.open(fileName, ios :: app);
if(NULL == outputFile)
{
//print error;
return -1;
}
但最后,文件内容为空,没有任何内容被写入。
任何想法为什么?
答案 0 :(得分:4)
您没有提供足够的信息来确定地回答问题,但以下是您可能遇到的一些问题:
您没有刷新ofstream
您没有关闭稍后尝试打开的文件(如果我是正确的,outputFile
是一个全局变量,因此它在程序结束前不会自动关闭)