【发布时间】:2012-05-14 15:14:06
【问题描述】:
我有这个方法应该获取缓冲区并将一些内容写入文件:
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
}
缓冲区内容为:
任务->缓冲区:elishaefla
任务->长度:10
我在另一个初始化函数中打开了流:
outputFile.open(fileName, ios :: app);
if(NULL == outputFile)
{
//print error;
return -1;
}
但最后,文件内容为空,没有写入任何内容。
知道为什么吗?
【问题讨论】:
-
你在什么时候检查的?请注意,它很可能正在缓冲,因此在检查之前您需要刷新或关闭流
-
更改参数为 ios::out | ios::app
-
@luskan:如果确实是 ofstream 则没关系: ios::out 保证会被 ORed 到标志中。
-
@luskan 仍然没有写任何东西...
-
删除了 c 标签,因为这纯粹是 c++ 问题