-1

here is the full code

#include <iostream>
#include <fstream>
#include<string>
using namespace std;

struct Employee {
    int UniqueID;
    string FirstName,LastName, EmailAddress;
    Employee* next;
    Employee* previous;
};

struct Company {
    Employee* head, * tail;
};


Company* InitializeDoubly() {
    Company* c = new Company;
    c->head = NULL;
    c->tail = NULL;
    return c;
}


bool isEmpty(Company *c){
    return(c->head == NULL);
}

void InsertAtHeadDoubly(Company* c, Employee e) {
    Employee* tmp = new Employee;
    if (isEmpty(c)) {

        tmp->UniqueID = e.UniqueID;
        tmp->FirstName = e.FirstName;
        tmp->LastName = e.LastName;
        tmp->EmailAddress = e.EmailAddress;
        tmp->next = NULL;
        tmp->previous = NULL;
        c->head = tmp;
        c->tail = tmp;
    }
    else {
        tmp->UniqueID = e.UniqueID;
        tmp->FirstName = e.FirstName;
        tmp->LastName = e.LastName;
        tmp->EmailAddress = e.EmailAddress;
        tmp->previous = NULL;
        tmp->next = c->head;
        c->head->previous = tmp;
        c->head = tmp;
    }
}

void DeleteFromDoublyHead(Company* c) {
    if (isEmpty(c)) {
        cout << "List is empty";
        return;
    }
    Employee* cur = c->head->next;
    delete c->head;
    c->head = cur;
    c->head->previous = NULL;
}




void tail2head(Company* c) {
    Employee* cur = c->tail;

    if (isEmpty(c)) {
        cout << "LIST IS EMPTY";
        return;
    }
    while (cur != NULL) {
        cout << cur->UniqueID << " " << cur->FirstName <<" " << cur->LastName <<" " << cur->EmailAddress << endl;
        cur = cur->previous;
    }
}



void parsefile(fstream& f,Company *c) {
    Employee efile;
    while (f) {
        
        getline(f, efile.FirstName,'\t');
        getline(f, efile.LastName, '\t');
        getline(f, efile.EmailAddress, '\t');
        f >> efile.UniqueID;
        InsertAtHeadDoubly(c, efile);
        f.ignore(INT_MAX, '\n');
    }
    DeleteFromDoublyHead(c);
    f.close();
}


void addemployee(fstream& f, Employee e1 ,Company* c) {
    
    int id = 8;
    f <<e1.FirstName << '\t' << e1.LastName << '\t' << e1.EmailAddress << '\t' << id;
    f.close();
}


int main()
{
    fstream f("file1.txt", ios::in | ios::out | ios::app);
    Company* company1 = InitializeDoubly();
    parsefile(f, company1);
    tail2head(company1);
    Employee e1;
    e1.FirstName = 'ali';
    e1.LastName = 'kilo';
    e1.EmailAddress ="ebla@gmail.com";
    addemployee(f, e1, company1);
    tail2head(company1);
    return 0;
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Ali Haydar
  • 13
  • 3
  • Have you *checked* the actual file and its contents? Perhaps the problem is not with the writing to the file, but rather how you add the data internally inside you program? Or how you display the contents? You use classes and functions which you don't show us, perhaps the problem is there? – Some programmer dude Dec 17 '21 at 09:51
  • By the way, are you sure you want to *close* the file in the `addemployee` function? And perhaps you should open the file in append mode so it doesn't overwrite the contents each time you start your program? – Some programmer dude Dec 17 '21 at 09:52
  • @Someprogrammerdude the file is working normally and I can take data from the file into C++ doubly linked list but Can't add from c++ to the file so idk – Ali Haydar Dec 17 '21 at 09:54
  • 1
    My ***guess***? You don't add the `Employee` to your `Company` object. – Some programmer dude Dec 17 '21 at 09:58
  • You should add the rest of your code to your question. – rturrado Dec 17 '21 at 09:58
  • I added the full code – Ali Haydar Dec 17 '21 at 10:13
  • At least the most relevant parts. Do you realize you're passing a `company` parameter to `addemployee` and never using it? – rturrado Dec 17 '21 at 10:16
  • @rturrado yes sorry that was a mistake but I removed it and nothing happened – Ali Haydar Dec 17 '21 at 10:17
  • @Someprogrammerdude I created a new file and tried the function on it and it worked so no idea why that happened but thanks anw – Ali Haydar Dec 17 '21 at 10:26

1 Answers1

1

You should enable file be growing. For example, use additional flag ios::app like this: fstream f("file1.txt", ios::in | ios::out | ios::app);

Evgeny
  • 1,072
  • 6
  • 6