0

so I have this line of code that I'm using for files:

inCreditSystem.seekp((num - 1) * sizeof(client));

// And sometimes I have this code
inCreditSystem.seekp((num - 1) * sizeof(ClientData));

where

fstream inCreditSystem("credit.dat", ios::in | ios::out | ios::binary);
ClientData client;

On it's own, my code works but I wanted to show my skills to my teacher(since this is a final exercice for my term) so I wanted to place this in a template. (I have succesfully placed write and read for files in a template)

//In FileFunctions class, I have these lines of code
template <class T> ostream& binary_seekp(ostream&, T&, int);
template <class T> ostream& binary_seekp2(ostream&, T&, int);

//And this is the rest of the code
template<class T>
ostream& FileFunctions::binary_seekp(ostream& stream, T& value, int num)
{
    return stream.seekp((num-1) * sizeof(value));
}

template<class T>
ostream& FileFunctions::binary_seekp2(ostream& stream, T& value, int num)
{
    return stream.seekp((num-1) * sizeof(T));
}

and then call the function like this

binary_seekp(inCreditSystem, client, num);
binary_seekp2(inCreditSystem, client, num);

I have two templates for seek because as you can see, one line of code gets the size of 'ClientData' and the other gets the size of 'Client'.

Keep in mind FileFunctions is another class. When I run this code and finalize a record, it wont show up when I print them but they will if I do it with that one line of code. Any clue why my code won't work?

timoleonn
  • 303
  • 1
  • 5
  • 12
  • 1
    Those two functions are exactly the same and does the exact same thing. `sizeof(value) == sizeof(T)`. – Some programmer dude Apr 04 '18 at 07:41
  • 2
    As for your problem, it's really hard to say anything without an [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). For example how do you check that the seeking and reading worked? And what is `ClientData`? Reading (and writing) raw binary data usually only works for [POD types](http://en.cppreference.com/w/cpp/concept/PODType). – Some programmer dude Apr 04 '18 at 07:42
  • I would know that reading and seeking work if it would show all records that are entered when I output them – timoleonn Apr 04 '18 at 07:44
  • 1
    Read again how and why to provide a [mcve]. Especially "It doesn't work" is not a sufficient description of your error. – chtz Apr 04 '18 at 07:53

0 Answers0