gpt4 book ai didi

c++ - Ofstream 不写入文件 C++

转载 作者:搜寻专家 更新时间:2023-10-31 01:15:01 32 4
gpt4 key购买 nike

我有这个方法应该获取缓冲区并将一些内容写入文件:

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;
}

但最后,文件内容是空的,什么都没有写入。

知道为什么吗?

最佳答案

您没有提供足够的信息来确定地回答问题,但以下是您可能面临的一些问题:

  1. 您没有刷新 ofstream

    的缓冲区
  2. 您没有关闭您稍后尝试打开的文件(如果我是正确的,outputFile 是一个全局变量,所以它不会自动关闭直到结束程序)

关于c++ - Ofstream 不写入文件 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10586127/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com