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();
}
File I/O. Writing to a binary file.
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:
But, it comes up with "Error: No overloaded function takes one parameter." If you can help, it would be much appreciated,
Thank you,
-Lenox
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:
Documentation is a wonderful resource, just so ya' know. [smile]
Database.write((char*)&Kills, sizeof(Kills));
orDatabase.write((char*)&Deaths, sizeof(Deaths));
Documentation is a wonderful resource, just so ya' know. [smile]
firs you need to set your file stream in binary mode e.g.:
to write out an integer it would be:
to read in an integer:
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.
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.
std::basic_ostream::write's first parameter is of type const char* not char* not that is matters that much but that is type you want cast to.
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?
EDIT2: Also, is there a way to write a negative int as just an int?
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) { /* ... */ }
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?
[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?
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));//....}
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?
[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?
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().
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement