rogersce/cnpy has 1,494 GitHub stars and 337 forks. GitHot measured 0 stars in 24 hours. Last checked 2026-11-08 05:07 UTC. Last push 2023-01-18.
library to read/write .npy and .npz files in C/C++
Measured star growth
- in 24h
- 0
- in 7 days
- Collecting history
- in 15 days
- Collecting history
- in 1 month
- Collecting history
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README.md
OriginalPurpose:
NumPy offers the save method for easy saving of arrays into .npy and savez for zipping multiple .npy arrays together into a .npz file.
cnpy lets you read and write to these formats in C++.
The motivation comes from scientific programming where large amounts of data are generated in C++ and analyzed in Python.
Writing to .npy has the advantage of using low-level C++ I/O (fread and fwrite) for speed and binary format for size. The .npy file header takes care of specifying the size, shape, and data type of the array, so specifying the format of the data is unnecessary.
Loading data written in numpy formats into C++ is equally simple, but requires you to type-cast the loaded data to the type of your choice.
Installation:
Default installation directory is /usr/local.
To specify a different directory, add -DCMAKE_INSTALL_PREFIX=/path/to/install/dir to the cmake invocation in step 4.
- get cmake
- create a build directory, say $HOME/build
- cd $HOME/build
- cmake /path/to/cnpy
- make
- make install
Using:
To use, #include"cnpy.h" in your source code. Compile the source code mycode.cpp as
g++ -o mycode mycode.cpp -L/path/to/install/dir -lcnpy -lz --std=c++11
Description:
There are two functions for writing data: npy_save and npz_save.
There are 3 functions for reading:
npy_loadwill load a .npy file.npz_load(fname)will load a .npz and return a dictionary of NpyArray structues.npz_load(fname,varname)will load and return the NpyArray for data varname from the specified .npz file.
The data structure for loaded data is below.
Data is accessed via the data<T>()-method, which returns a pointer of the specified type (which must match the underlying datatype of the data).
The array shape and word size are read from the npy header.
struct NpyArray {
std::vector<size_t> shape;
size_t word_size;
template<typename T> T* data();
};
See example1.cpp for examples of how to use the library. example1 will also be build during cmake installation.
Explore related projects
codecrafters-io/build-your-own-x
Master programming by recreating your favorite technologies from scratch.
560,836 starspublic-apis/public-apis
A collective list of free APIs
497,082 starsmorluto/rea
Reverse engineer anything with agents, from app behavior down to native binaries.
441,526 starsopenclaw/openclaw
The AI that really does things. Any OS. Any Platform. The lobster way. 🦞
394,157 stars