我正在尝试写入以将数据追加到文件的末尾,并正在使用seekp(streamoff off,ios_base :: seekdir dir)函数,但它没有追加,以某种方式将数据写入文件的中间。
我试图添加打开这样的文件-file.open(resultFile,fstream :: in | fstream :: out);
(如在其他类似的职位建议),尽管我仍然得到相同的输出。
那是代码:

bool Manager::ValidCommand(Command* com, ofstream &ResultFile) const
{
    Employee::DepartmentEn dept = Employee::InvalidDepartment;
       if (com == NULL)
            return false;
       if(com->GetFunction() <Command::PrintCityCouncilList || com->GetFunction() > Command::HireEmployee){
            ResultFile.seekp(0,ios::end);
            ResultFile << "Command:Failed activating function - invalid function number\n";
            return false;}
       if ((com->GetFunction() == Command::PrintDepartmentEmployees) || (com->GetFunction() == Command::PrintDepartmentExpenses) || (com->GetFunction() == Command::PrintDepartmentStatistics)){
            dept = com->GetDepartment();
       if((pcc->FindDepartment(dept) == NULL )|| (dept < Employee::Engineering) ||(dept > Employee::Sanitation))
       {
           ResultFile.seekp(0,ios::end);
           ResultFile << "Command:Failed activating function - invalid department \n";
           return false;
       }
   }
   return true;
}


我可能做错了什么?

最佳答案

代码中引用的变量pcc在这里做什么?

if((pcc->FindDepartment(dept) == NULL ) .....))
       {  ....   }




根据本文档有关C ++的文件输入/输出here的引用


  os :: app所有输出操作都在文件末尾执行,将内容附加到文件的当前内容。此标志只能在为仅输出操作打开的流中使用。


这意味着,如果同时指定了两种输入/输出模式,则添加将不起作用。您可以确认吗?如果是这种情况,可能值得忘记以文本模式打开,而改用二进制模式。

另一件事-是否确实打开了文件以便您查找ResultFile.seekp(...)?通过刷新调试消息来检查流的值,如下所示:

if (ResultFile.bad()) cout << "Bad stream!\n";

09-08 00:20