write and read string binary file c 2b 2b

Solutions on MaxInterview for write and read string binary file c 2b 2b by the best coders in the world

showing results for - "write and read string binary file c 2b 2b"
Esteban
17 Feb 2016
1#include <cstring>
2#include <iostream>
3#include <fstream>
4#include <string>
5
6struct player_data
7{
8  std::string name;
9  int score;
10};
11
12int main()
13{
14
15  std::ofstream savefile("score.dat", std::ios_base::binary);
16  if(savefile.good())
17  {
18    player_data Player1;
19    Player1.name = "John Doell";
20    Player1.score = 55;
21    savefile.write(Player1.name.c_str(),Player1.name.size()); // write string to binary file
22    savefile.write("\0",sizeof(char)); // null end string for easier reading
23    savefile.write(reinterpret_cast<char*>(&Player1.score),sizeof(Player1.score)); // write int to binary file
24    savefile.close();
25  }
26
27  std::ifstream loadfile("score.dat", std::ios_base::binary);
28  if(loadfile.good())
29  {
30    player_data Player1;
31    std::getline(loadfile,Player1.name,'\0'); // get player name (remember we null ternimated in binary)
32    loadfile.read((char*)&Player1.score,sizeof(Player1.score)); // read int bytes
33    std::cout << "Player1 name: " << Player1.name << std::endl;
34    std::cout << "Player1 score: " << Player1.score << std::endl;
35  }
36
37  return 0;
38}
queries leading to this page
write binary file c 2b 2bread binary of file in c 2fc 2b 2bofstream write string to binary filewrite file binary c 2b 2bread string from binary file c 2b 2breading a binary file using fread c 2b 2bc 2b 2b fwrite binary filewrite string to binary file c 2b 2bhow to read binary data from file in c 2b 2bhow to read binary file cppc 2b 2b filesystem read binary filewrite binary string to file c 2b 2bhow to write in binary file c 2b 2bopen a binary file in read and write mode in c 2b 2bread and write to binary file in c 2b 2bbinary file c 2b 2bc 2b 2b open binary filecpp read binary filewrite in binary file c 2b 2bcpp write to binary filehow to read struct from binary file in c 2b 2bcpp write binary fileopen binary file c 2b 2bhow to write binary data to a file c 2b 2bcpp reading binary fileswrite to binary file c 2b 2bexample of binary file and text file in c 2b 2bhow to write data to a binary file in c 2b 2bc 2b 2b read file binary dataopen a file in binary c 2b 2bc 2b 2b file write to binary does not workc 2b 2b write binary data to filecan you write binary files with c 2b 2bbinary file in c 2b 2bprogram to read and write a binary file in c 2b 2bc 2b 2b write to end of binary filec 2b 2b write to a binary fileopen binary file in c 2b 2bsave binary file in c 2b 2bc 2b 2b execute a binary filec 2b 2b binary read fileread a binary file c 2b 2buse binary file in c 2b 2bhow to store binary files c 2b 2bc 2b 2b binary file read stringc 2b 2b open and read a binary fileread binary file in c 2b 2bread file binary c 2b 2bload from binary file cpphow to write binary data to file using ofstream c 2b 2bfile opening in c 2b 2b for text and binary file 3fc 2b 2b read file as binaryhow do i open a file in binary in c 2b 2bc 2b 2b reading binary filec 2b 2b write binary fileread binary file c 2b 2bhow to convert binary file in c 2b 2bwrite binary file c 2b 2bc 2b 2b read binary filec 2b 2b how to read binary filesreading binary file c 2b 2bread from binary file c 2b 2bc 2b 2b binary file writing and readingc 2b 2b read file binaryc 2b 2b binary files readread binary string in c 2b 2b filehow to write string to binary file c 2b 2bc 2b 2b open a binary filec 2b 2b write to file binaryhow to write to binary file c 2b 2bc 2b 2b write binary data filehow to change text file in to binary file in c 2b 2bc 2b 2b file binaryread a binary file in c 2b 2bhow to load things from a binary file in c 2b 2bwrite binary data to file c 2b 2bwrite a binary file c 2b 2bc 2b 2b write to binary filehow to write a binary file c 2b 2bc 2b 2b read text file as binaryc 2b 2b open binary file for readingwrite and read string binary file c 2b 2b