Page 1 of 1

Ben Uses C++

PostPosted: 23 Mar 2015, 17:04
by Ben
Okay so I'm taking a course on C++ and I'm really enjoying it, but once in a while I have some problems :) asking around on forums has helped a lot, but sometimes people are very unhelpful to noobs :P
So I figure maybe you guys could help me once in a while if you have the knowledge :D

Anyway, my first problem is figuring out how to write to a binary file. Basically, I am converting a program I made earlier from read and write to a text file so that it now uses binary.
In the ext file version, I used a structure to and cin.getline within a function to write to the text file. I'm having trouble figuring out what the "binary equivalent" of this would be:
  Code:
struct Player { char name[ML]; char number[ML]; int points; }; ... void addPlayer(Player s[], int &num) { cout << "Player Name: "; cin >> ws; cin.getline(s[num].name, ML); //gets player name cout << "Player Number: "; cin.getline(s[num].number, ML); // gets player number cout << "Points Scored :"; cin >> s[num].points, ML; //gets points scored by player num++; //number increases so the next time the function is used it makes a new player instead of overwriting the old one return; }
Any tips/pointing me to a good source would be great! In the meantime, I'll continue looking through my book and google...

Re: Ben Uses C++

PostPosted: 23 Mar 2015, 19:47
by Krom
Maybe this interests you:
  Code:
/// stdio file implementation. class FileIO : public duFileIO { FILE* m_fp; int m_mode; public: FileIO(); virtual ~FileIO(); bool openForWrite(const char* path); bool openForRead(const char* path); virtual bool isWriting() const; virtual bool isReading() const; virtual bool write(const void* ptr, const size_t size); virtual bool read(void* ptr, const size_t size); };
  Code:
FileIO::FileIO() : m_fp(0), m_mode(-1) { } FileIO::~FileIO() { if (m_fp) fclose(m_fp); } bool FileIO::openForWrite(const char* path) { if (m_fp) return false; m_fp = fopen(path, "wb"); if (!m_fp) return false; m_mode = 1; return true; } bool FileIO::openForRead(const char* path) { if (m_fp) return false; m_fp = fopen(path, "rb"); if (!m_fp) return false; m_mode = 2; return true; } bool FileIO::isWriting() const { return m_mode == 1; } bool FileIO::isReading() const { return m_mode == 2; } bool FileIO::write(const void* ptr, const size_t size) { if (!m_fp || m_mode != 1) return false; fwrite(ptr, size, 1, m_fp); return true; } bool FileIO::read(void* ptr, const size_t size) { if (!m_fp || m_mode != 2) return false; size_t readLen = fread(ptr, size, 1, m_fp); return readLen == 1; }
It allows to write any data to a file as long as you can get a pointer to it ans a size in bytes:
  Code:
io->write(&maxDistance, sizeof(maxDistance)); io->write(&maxRegions, sizeof(maxRegions));

Re: Ben Uses C++

PostPosted: 24 Mar 2015, 19:48
by RandomLyrics
I use, for saving txt files:
  Code:
#include <fstream> //GLOBAL ofstream mlog; int main (){ mlog.open("example.txt"); mlog << "lalalalalla" << "\t" << endl; mlog.close(); }

Re: Ben Uses C++

PostPosted: 24 Mar 2015, 21:08
by Ben
Thanks for your help, Krom, but it doesn't make much sense to me :D That's the same problem I was having on other forums: No one understands where I am at in my programming skill. My professor encourages us to go online for solutions, but it's frustrating when people are throwing code at me I've never seen before :(

Re: Ben Uses C++

PostPosted: 24 Mar 2015, 23:41
by RandomLyrics
Coz u r not specify and not precisely describe your problem :P Or im dumb xDD
Plz tell, what u want, what Inputs and what are the desirable Outputs.
heres something about binary files: http://courses.cs.vt.edu/~cs2604/fall00/binio.html

Re: Ben Uses C++

PostPosted: 25 Mar 2015, 03:10
by Ben
Yeah I read that document but it wasn't very helpful. I mean it helped a very little bit :P

What I'm trying to do is make a program that will keep track of soccer players (the player name, player number, and points scored by player). When the program is over, all the information will be saved to an output file. When the program reopens, the previous data from the output file will be loaded and new data will be appended to the output file.

Re: Ben Uses C++

PostPosted: 25 Mar 2015, 05:21
by Krom
@Ben: Then you don't need to store binary data, keep everything as text :-P

Re: Ben Uses C++

PostPosted: 25 Mar 2015, 05:43
by zombie01
@Ben: Then you don't need to store binary data, keep everything as text :-P
agreed

and with this
  Code:
// writing on a text file #include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile ("example.txt"); if (myfile.is_open()) { myfile << "This is a line.\n"; myfile << "This is another line.\n"; myfile.close(); } else cout << "Unable to open file"; return 0; }
you can write to files

since fstreams are streams you can simply use << to stream the data into it. just use a strict format.

and with this you can read files
  Code:
// reading a text file #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( getline (myfile,line) ) { cout << line << '\n'; } myfile.close(); } else cout << "Unable to open file"; return 0; }
the fstreams are similar to cin and cout.
you have to create a stream that links to a txt file ( ifstream myfile ("example.txt") )
and then open the stream. ( if (myfile.is_open()) )
then you keep getting the information till the end of the file ( while ( getline (myfile,line) { cout << line << '\n'; )
at the end you have to close the stream again myfile.close();

if you want it in binary, you can use
  Code:
#include <string> #include <bitset> #include <iostream> using namespace std; int main(){ string myString = "Hello World"; for (std::size_t i = 0; i < myString.size(); ++i) { cout << bitset<8>(myString.c_str()[i]) << endl; } }
basiaclly, you have the string "Hello World" and per letter of the string it will convert it to an 8bit code

output:
  Code:
01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100
and
  Code:
char(binary.to_ulong())
should change it back to a char
where binary is of the type bitset<8>

Re: Ben Uses C++

PostPosted: 25 Mar 2015, 13:55
by Ben
@Ben: Then you don't need to store binary data, keep everything as text :-P
Although that "works" the propose of the exercise is to learn how to work with binary files. If I keep the text format, I'll fail ;)

Zombie, I'll take a look at your post when I am on my computer :)

Re: Ben Uses C++

PostPosted: 25 Mar 2015, 19:19
by zombie01
I think you try to find some way with google for a binary file.
My solution is pretty unusual and I dont know if it works

Re: Ben Uses C++

PostPosted: 26 Mar 2015, 05:37
by Krom
My posted code looks lengthy, but if you scrap the fluff and extract just the parts required for read.write binary data you will see they are quite short

Re: Ben Uses C++

PostPosted: 04 Jul 2015, 17:08
by Michalpl
c++ Looks familiar to sm