Public Group

File I/O. Writing to a binary file.

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

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 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 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));

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 on other sites
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.

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 on other sites
Quote:
 Original post by LenoxEDIT: 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 LenoxEDIT2: 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 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 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 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 on other sites
Quote:
 Original post by LenoxAlso, 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().

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 104
• 11
• Forum Statistics

• Total Topics
632977
• Total Posts
3009680
• Who's Online (See full list)

There are no registered users currently online

×