Sign in to follow this  

File I/O. Writing to a binary file.

This topic is 4715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I have a problem. I'm trying to program my own customized database (only not in a dll this time :P). I've come across a problem, I can't write an int to a file. So, my question is, is it possible to write an int to a binary file? If so, how? Here is what I have so far:
int AddPlayer(char Playername[10],
              int Kills,
              int Deaths)
{
    ofstream Database("Database\\PlayerDatabase.Sinex", ios::app | ios::out)
    if(!Playername)
    {
       return 0;      
    };
    Database.write(Playername, 10);
    if(Kills)
    {
          Database.write(Kills);
    };
    if(Deaths)
    {
          Database.write(Deaths);
    };
    Database.close();
}

But, it comes up with "Error: No overloaded function takes one parameter." If you can help, it would be much appreciated, Thank you, -Lenox

Share this post


Link to post
Share on other sites
If you look at the documentation for std::ostream::write(), you'll see that it requires two parameters, the address of the data to write, and the number of bytes to write. So what you need to say is something like:
Database.write((char*)&Kills, sizeof(Kills));
or
Database.write((char*)&Deaths, sizeof(Deaths));

Documentation is a wonderful resource, just so ya' know. [smile]

Share this post


Link to post
Share on other sites
firs you need to set your file stream in binary mode e.g.:


std::ofstream ofs("file-name", ios::app | ios::binary);


to write out an integer it would be:

int foo = 0;
//....
ofs.write(reinterpret_cast<const char*>(&foo), sizeof(int));


to read in an integer:

int foo = 0;
//...
ifs.read(reinterpret_cast<char*>(&foo), sizeof(int));


and whats with all the if statements? Playername can never be null, and to not write out Kills/Deaths to file if its value is 0 will mess with your file format, you should write it regardless of its value if you want to be able to read your whole file back properly.

NOTE: You don't have to read/write one object at time, read/write are for bulk unformatted I/O operations.

Share this post


Link to post
Share on other sites
Oops, forgot to add in the ios::binary on that example, also, all the if statements are in case something goes wrong and a null value is sent, I just forgot the else's. EDIT: I guess I really only do need just the Playername one.

EDIT2: Also, is there a way to write a negative int as just an int?

Share this post


Link to post
Share on other sites
Quote:
Original post by Lenox
EDIT: I guess I really only do need just the Playername one.


no, if Playername is of type character array (not a pointer to an array) it can never be null.

Quote:
Original post by Lenox
EDIT2: Also, is there a way to write a negative int as just an int?


if you want to force only positive values because it does not make sense to have negatives ones aswell then use unsigned integer types e.g.


int AddPlayer(char Playername[10],
unsigned int Kills,
unsigned int Deaths) { /* ... */ }

Share this post


Link to post
Share on other sites
Aiight, thanks. I understand now. Thank you all! :P

[EDIT]

Also, please tell me if I have this right, but if I use unsigned int, then I will be able to receive a -int?

Share this post


Link to post
Share on other sites
Quote:
Original post by Lenox
[EDIT]
Also, please tell me if I have this right, but if I use unsigned int, then I will be able to receive a -int?


you can assign a negative integer to an unsigned int but it wont do what you think, i'm not sure what problem your having? are trying to avoid writing negative integers to file or not?

if you want to use signed integers but dis-regard the sign when you write out to binary file then you can use the function std::abs in the header cmath so for example you pass -1 to abs it will return +1, if you pass +1 it will return +1 e.g.:


#include <cmath>
//....

int AddPlayer(char Playername[10],
int Kills,
int Deaths) {

//.....
int foo = std::abs(Kills);
ofs.write(reinterpret_cast<const char*>(&foo), sizeof(int));
//....
}

Share this post


Link to post
Share on other sites
No, I'm trying to make sure I can add the +/- signs on there. Like +1 kills, -10 experience.

[EDIT]

Also, while we're on the subject of File I/O, is there a function I can use to find a specific word or phrase?

Share this post


Link to post
Share on other sites
Quote:
Original post by Lenox
Also, while we're on the subject of File I/O, is there a function I can use to find a specific word or phrase?


Load the file in memory, e.g. into a std::string (or a vector, or a rope or use a memory-mapped file), and use std::string::find().

Share this post


Link to post
Share on other sites

This topic is 4715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this