C++ 以读取模式打开文件时出现文件处理问题

C++ 以读取模式打开文件时出现文件处理问题,c++,data-structures,file-handling,C++,Data Structures,File Handling,我有一个任务,从用户那里获取一个输入,并将其写入文件中,并成功地编写了文件,就像下面给出的函数一样,它工作得很好 void add_category(字符串名称、int ID_category) { 类别*新节点; newnode=新类别; 新建节点->类别名称=名称; newnode->id=id\u类别; if(headd==NULL){ headd=新节点; newnode->next=NULL; newnode->pre=NULL; lastd=newnode; } 否则{ lastd-

我有一个任务,从用户那里获取一个输入,并将其写入文件中,并成功地编写了文件,就像下面给出的函数一样,它工作得很好

void add_category(字符串名称、int ID_category)
{
类别*新节点;
newnode=新类别;
新建节点->类别名称=名称;
newnode->id=id\u类别;
if(headd==NULL){
headd=新节点;
newnode->next=NULL;
newnode->pre=NULL;
lastd=newnode;
}
否则{
lastd->next=newnode;
newnode->pre=lastd;
newnode->next=NULL;
lastd=newnode;
}
dlength++;
流文件(“Category.txt”,ios::app);
类别*温度=头部D;
类别*2;
while(temp!=NULL){

_类别的文件名_不要从同一个文件中多次读取数据,只需将其读入内存一次,然后处理它

using Category = std::pair<int, std::string>;
std::vector<Category> categories;

{
  Category cat;
  std:ifstream file("Category.txt");

  while (file >> cat.first >> cat.second)
    categories.push_back(cat);
}

auto novels = std::find_if(std::begin(categories), std::end(categories),
    [](const Category &category) { return category.second == "novels"; });
使用Category=std::pair;
std::向量类别;
{
类别猫;
std:ifstream文件(“Category.txt”);
while(文件>>cat.first>>cat.second)
类别。推回(cat);
}
自动小说=标准::查找如果(标准::开始(类别),标准::结束(类别),
[](常量类别和类别){return Category.second==“小说”;});

在我看来,问题在于ios::app
模式的使用。请参阅。
app
模式用于追加不写入,因此发生的情况是文件指示符位于文件末尾。
我希望这有助于您解决问题。

您需要复习,请提供。您是对的,但我不能在作业中使用向量