0

I have an assignment to take an input from the user and wrote it in file and successfully wrote the file like the function given below,and it is working nicely

void add_category(string NAME, int ID_category)
{
    category* newnode;
    newnode = new category;
    newnode->name_of_category = NAME;
    newnode->id = ID_category;
    if (headd == NULL) {
        headd = newnode;
        newnode->next = NULL;
        newnode->pre = NULL;
        lastd = newnode;
    }
    else {
        lastd->next = newnode;
        newnode->pre = lastd;
        newnode->next = NULL;
        lastd = newnode;
    }
    dlength++;

    ofstream file("Category.txt", ios::app);
    category* temp = headd;
    category* temp2;
    while (temp != NULL) {
        file << temp->name_of_category << endl;
        file << temp->id << endl;
        temp2 = temp;
        temp = temp->next;
        delete temp2;
    }
    file.close();
}

But the problem is that while opening a file by code get crashed or TBH I don't how I can open a file. I am using double linked list. My task is to open a file in a read mode and delete the category entered by the user. Here is my function of deleting the category:

void delete_category(string remove_name)
{
    category* currentptr = headd;
    category* temp;
    category* temp2;
    category* temp3 = headd;

    ifstream file("Category.txt");

    if (!file) {
        cout << "file can not open" << endl;
    }
    else {
        while (temp3 != NULL && !file.eof()) {
            file >> temp3->name_of_category;
            file >> temp3->id;
            temp3 = temp3->next;
        }
    }

    int save = search_category(remove_name);

    cout << save << endl;

    if (dlength <= 0) {
        cout << " NO category found" << endl;
    }
    else {
        if (save == 0) {
            temp = headd;
            headd = headd->next;
            headd->pre = NULL;
        }
        else {
            for (int i = 1; i < save; i++) {
                currentptr = currentptr->next;
            }

            temp = currentptr->next;
            temp2 = temp->next;

            currentptr->next = temp2;
            temp2->pre = currentptr;
        }

        dlength--;

        delete temp;

        cout << " Successfully Deleted the Category" << endl;
    }
}

As I have called the searched function in the deleting category, I have used the same method for opening a file in search function:

int search_category(string search)
{
    category* currentptr = headd;
    int flag = 0;
    category* temp3 = headd;

    ifstream file("Category.txt");

    if (!file) {
        cout << "file can not open" << endl;
    }
    else {
        while (temp3 != NULL && !file.eof()) {

            file >> temp3->name_of_category;
            file >> temp3->id;

            temp3 = temp3->next;
        }
    }

    for (int i = 0; i < dlength; i++) {
        if (currentptr->name_of_category == search) {
            flag = 1;
            return i;
        }

        currentptr = currentptr->next;
    }

    if (flag == 0) {
        cout << " Sorry not found" << endl;
        return -1;
    }
}

Please correct me where I am making a mistake to open a file.

Adrian Mole
  • 30,672
  • 69
  • 32
  • 52
josh
  • 9
  • 2
  • You will want to review [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) Please provide [A Minimal, Complete, and Verifiable Example (MCVE)](http://stackoverflow.com/help/mcve). – David C. Rankin Jan 05 '20 at 07:27

2 Answers2

0

Instead of reading data many times from the same file, just read it once into memory and work on it.

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"; });
chwala
  • 179
  • 10
0

In my opinion, the problem is the usage of ios::app mode. Refer to link. app mode is used for appending not writing, so what happened is that the file indicator is positioned at the end of the file. I hope this helps you solve your problem.

Mohammed Deifallah
  • 1,263
  • 1
  • 9
  • 19