1

I'm using binary file to update a record, the code: file.seekp(file.tellp()-sizeof(*this)); shows an error line under the minus sign. The error message is :

Error C2666 'std::fpos<_Mbstatet>::operator -': 3 overloads have similar conversions BarbershopDemo c : \users...\employeeinfo.cpp 133

void Employee::updateEmployee(char *t) {
fstream file;
file.open("file1.dat", ios::in | ios::out | ios::ate | ios::binary);
file.seekg(0);
file.read((char*)this, sizeof(*this));
while (!file.eof()) {
    if (!strcmp(t, eFirstName)) {
        getEmployeeData();
        file.seekp(file.tellp()-sizeof(*this));
        file.write((char*)this, sizeof(*this));
    }
    file.read((char*)this, sizeof(*this));
   }
   file.close();
   }    
void Employee::updateEmployee(char *t) {
  fstream file;
file.open("file1.dat", ios::in | ios::out | ios::ate | ios::binary);
file.seekg(0);
file.read((char*)this, sizeof(*this));
while (!file.eof()) {
    if (!strcmp(t, eFirstName)) {
        getEmployeeData();
        file.seekp(file.tellp()-sizeof(*this));
        file.write((char*)this, sizeof(*this));
    }
    file.read((char*)this, sizeof(*this));
}
file.close();
}

I am not sure what is causing the error. Thanks for the help. what I included in my project.

#include "stdafx.h"
#include "EmployeeInfo.h"
#include<iostream>
#include <string.h>
#include <fstream>
#include<stdio.h>
#include<conio.h>
using namespace std;
  • I think maybe you want `file.seekp(-sizeof(*this), std::ios::cur);`? – Galik Apr 29 '18 at 17:55
  • If this code works, it is only by luck. There is (probably, we'd have to see the header file to be sure) no guarantee that the representation in memory that the implementation happens to use for instances of class `Employee` will make sense when read back in. For example, if it contains a `std::string`, it will definitely not work. – David Schwartz Apr 29 '18 at 18:04
  • [Why `while (!file.eof())` is wrong](https://stackoverflow.com/q/5605125/9254539). – BessieTheCookie Apr 29 '18 at 18:57
  • the tutorial had it that way. – Person23456 Apr 29 '18 at 19:47
  • @Person23456 - The tutorial is just nuts. One example: opening the file with `ios::ate` positions at the end of the file. Then it immediately does `seekg(0)` to go to the beginning. What?! You are wasting both your own *and* our time by following that tutorial. I recommend [a book where the text has been reviewed.](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) – Bo Persson Apr 29 '18 at 20:38

0 Answers0