<jupyter_start><jupyter_text>Archivos Archivos Binarios o de Acceso Aleatorio Escribir a un archivo binario<jupyter_code>%%writefile writebinfile.cpp
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    ofstream fout("archivo1.bin");

    char a = 'A';
    fout.write(&a, sizeof(char));

    int b = 1981;
    fout.write((char*)&b, sizeof(int));

    long long c = 123451234512351235;
    fout.write((char*)&c, sizeof(long long));

    float d = 12.49f;
    fout.write((char*)&d, sizeof(float));

    double e = 1.9e-20;
    fout.write((char*)&e, sizeof(double));

    string f = "Todos mis amigos jalaron feo";
    fout.write(f.c_str(), sizeof(char)*f.size()); // no podemos escribir string directamente sino char*

    float g[5] = { 1.1f, 2.2f, 3.3f, 4.4f, 5.5f };
    fout.write((char*)g, sizeof(float)*5);

    fout.close();

    return 0;
}
%%script bash
g++ writebinfile.cpp && ./a.out && hexdump -C archivo1.bin
ls -l<jupyter_output>00000000  41 bd 07 00 00 03 30 31  64 3e 96 b6 01 0a d7 47  |A.....01d>.....G|
00000010  41 6e 58 f1 cb 65 6e d6  3b 54 6f 64 6f 73 20 6d  |AnX..en.;Todos m|
00000020  69 73 20 61 6d 69 67 6f  73 20 6a 61 6c 61 72 6f  |is amigos jalaro|
00000030  6e 20 66 65 6f cd cc 8c  3f cd cc 0c 40 33 33 53  |n feo...?...@33S|
00000040  40 cd cc 8c 40 00 00 b0  40                       |@...@...@|
00000049
total 28
-rwxr-xr-x 1 root root 13944 Aug 25 18:54 a.out
-rw-r--r-- 1 root root    73 Aug 25 18:54 archivo1.bin
drwxr-xr-x 1 root root  4096 Aug 24 16:35 sample_data
-rw-r--r-- 1 root root   686 Aug 25 18:53 writebinfile.cpp<jupyter_text>Leer datos de un archivo binario<jupyter_code>%%writefile readinfile.cpp
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    ifstream fin("archivo1.bin");

    char a;
    fin.read(&a, sizeof(char));

    int b;
    fin.read((char*)&b, sizeof(int));

    long long c;
    fin.read((char*)&c, sizeof(long long));

    float d;
    fin.read((char*)&d, sizeof(float));

    double e;
    fin.read((char*)&e, sizeof(double));

    char big[29] = { 0 };
    fin.read(big, sizeof(char)*28); // debemos saber exactamente cuantos caracteres son
    string f(big);

    float g[5];
    fin.read((char*)g, sizeof(float)*5);

    fin.close();

    cout << a << endl;
    cout << b << endl;
    cout << c << endl;
    cout << d << endl;
    cout << e << endl;
    cout << f << endl;
    for (int i = 0; i < 5; ++i) {
        cout << g[i] << " ";
    }

    return 0;
}
%%script bash
g++ readinfile.cpp && ./a.out<jupyter_output>A
1981
123451234512351235
12.49
1.9e-20
Todos mis amigos jalaron feo
1.1 2.2 3.3 4.4 5.5<jupyter_text>Escribir structs en archivo binario<jupyter_code>%%writefile savestructbin.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>

using namespace std;

struct Product {
    int     id;
    float   cost;
    char    desc[100];
};

Product newProd(int id, float cost, const char* desc) {
    Product p;
    p.id = id;
    p.cost = cost;
    strcpy(p.desc, desc);

    return p;
}

int main() {
    ofstream f("products.bin");
    Product p[5];
    
    p[0] = newProd(1, 1.5, "Pan con tamal");
    p[1] = newProd(2, 1.0, "Quinoa");
    p[2] = newProd(3, 1.2, "Pan con camote");
    p[3] = newProd(4, 2.5, "Pan con lomito");
    p[4] = newProd(5, 1.8, "Pan con palta");

    int n = 5;
    f.write((char*)&n, sizeof(int));
    f.write((char*)p, sizeof(Product)*n);

    f.close();

    return 0;
}
%%script bash
g++ savestructbin.cpp && ./a.out && hexdump -C products.bin<jupyter_output>00000000  05 00 00 00 01 00 00 00  00 00 c0 3f 50 61 6e 20  |...........?Pan |
00000010  63 6f 6e 20 74 61 6d 61  6c 00 00 00 00 00 00 00  |con tamal.......|
00000020  00 00 00 00 e0 e9 9c 33  66 7f 00 00 f0 fe 66 33  |.......3f.....f3|
00000030  66 7f 00 00 40 dd d3 67  fc 7f 00 00 5a 97 c5 33  |f...@..g....Z..3|
00000040  66 7f 00 00 01 f9 9c 33  66 7f 00 00 06 00 00 00  |f......3f.......|
00000050  00 00 00 00 c0 f9 9c 33  66 7f 00 00 00 dd d3 67  |.......3f......g|
00000060  fc 7f 00 00 b8 ea 9c 33  66 7f 00 00 c0 f9 9c 33  |.......3f......3|
00000070  02 00 00 00 00 00 80 3f  51 75 69 6e 6f 61 00 20  |.......?Quinoa. |
00000080  74 61 6d 61 6c 00 00 00  00 00 00 00 00 00 00 00  |tamal...........|
00000090  e0 e9 9c 33 66 7f 00 00  f0 fe 66 33 66 7f 00 00  |...3f.....f3f...|
000000a0  40 dd d3 67 fc 7f 00 00  5a 97 c5 33 66 7f 00 00  |@..g....Z..3f...|
000000b0  01 f9 9c 33 66 7f 00 00  06 00 00 00 00 00 00 00  |...3f...........|
000000c0  c0 f9 9c 33 66 7f 00 00  00 dd d3 67 fc 7f[...]<jupyter_text>Leer structs de archivo<jupyter_code>%%writefile readstructbin.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>

using namespace std;

struct Product {
    int     id;
    float   cost;
    char    desc[100];
};

int main() {
    ifstream f("products.bin");
    Product p[5];

    f.seekg(0, f.end);
    int size = f.tellg();
    f.seekg(0, f.beg);

    cout << "File size: " << size << " bytes\n";
    
    int n;
    f.read((char*)&n, sizeof(int));
    f.read((char*)p, sizeof(Product)*n);

    f.close();

    for (int i = 0; i < n; ++i) {
        cout << p[i].id << "\t"
             << p[i].cost << "\t"
             << string(p[i].desc) << endl;
    }

    return 0;
}
%%script bash
g++ readstructbin.cpp && ./a.out
ls -l<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16790077748981858, 'fraction_numerical': 0.14383561643835616, 'mean_word_length': 2.4152970922882426, 'pattern_counts': {'":': 0, '<': 76, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '587315', 'license': 'CC0-1.0', 'n_tokens_mistral': 2845, 'n_tokens_neox': 2201, 'n_words': 718, 'path': '20202/aed_20202_0201_files.ipynb', 'repo_name': 'pcsilcan/aed', 'size': '5402'}