Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct matrix
- {
- int** tab;
- unsigned w;
- unsigned h;
- };
- typedef struct matrix Matrix;
- void init(Matrix* m, unsigned w, unsigned h)
- {
- unsigned i;
- m->w = w;
- m->h = h;
- m->tab = (int**)malloc(w*sizeof(int*));
- for(i = 0; i < w; ++i)
- m->tab[i] = (int*)malloc(h*sizeof(int));
- }
- void clean(Matrix* m)
- {
- unsigned i;
- for(i = 0; i < m->w; ++i)
- free(m->tab[i]);
- free(m->tab);
- }
- void save(Matrix* m, const char* path)
- {
- unsigned i;
- FILE* f = fopen(path, "wb");
- for(i = 0; i < m->w; ++i)
- fwrite((m->tab[i]), sizeof(int), m->h, f);
- fclose(f);
- }
- void load(Matrix* m, const char* path)
- {
- unsigned i;
- FILE* f = fopen(path, "rb");
- for(i = 0; i < m->w; ++i)
- fread((m->tab[i]), sizeof(int), m->h, f);
- fclose(f);
- }
- void fill(Matrix* m, int f)
- {
- unsigned i, j;
- for(i = 0; i < m->w; ++i)
- for(j = 0; j < m->h; ++j)
- m->tab[i][j] = f;
- }
- void display(Matrix* m)
- {
- unsigned i, j;
- for(i = 0; i < m->w; ++i)
- {
- for(j = 0; j < m->h; ++j)
- {
- printf("%d ", m->tab[i][j]);
- }
- printf("\n");
- }
- }
- int main(int argc, char** argv)
- {
- Matrix src, dst;
- init(&src, 5, 5);
- init(&dst, 5, 5);
- fill(&src, 13);
- fill(&dst, 0);
- display(&src);
- display(&dst);
- printf("\n\n");
- save(&src, "pliczek.lol");
- load(&dst, "pliczek.lol");
- display(&dst);
- clean(&src);
- clean(&dst);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement