-1

Hi I want to read some information in a struct that i wrote in file with fwrite but there's a problem I can't extract these information. I got 2 file

tttfs.h :

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

typedef struct _disk disk;
typedef struct _block block;

struct _block{
    uint8_t *unBlock;
};

struct _disk{
    int id;
    block *diskBlock;
};

tfs_create.c :

#include "tttfs.h"

uint8_t little[4];

int tttfs_create(int size, char *name);
void inttolitend(uint32_t x, uint8_t* lit_int);

int main(){
    tttfs_create(7, "disk.tfs");
    int f = 0;
    if((f=open("disk.tfs",O_RDONLY)) < -1) //I took this from an example.
       return 1;
    disk *d = malloc(sizeof(disk)); //Create a disk
    d->diskBlock = malloc(1024); //The block where I want to write the information from the file.
    lseek(f,0,SEEK_SET);//I want to read from the beginning
    read(f,d->diskBlock,1024); //I write all the information from the beginning to 1024th byte of my file
    int i;
    for(i=0; i<4; i++){
    printf("%d\n", (uint8_t)&d->diskBlock[i]);//print my result.
    }
}

int tttfs_create(int size, char *name){
    FILE *f = NULL;
    if ((f = fopen(name, "wb"))!=NULL)   /** si ouverture ok **/
    {
    disk *d = malloc(sizeof(disk));
    d->diskBlock = malloc(sizeof(block) * size);
    d->id = 1;
    int i;
    for(i = 0; i<size; i++){
       d->diskBlock[i].unBlock = malloc(sizeof(uint8_t) * 1024);
    }
    inttolitend(size, little);
    for(i = 0; i<4; i++){
       d->diskBlock[0].unBlock[i] = little[i];
    }
    for(i = 0; i<size; i++){
       fwrite(&d->diskBlock[i],sizeof(block),1,f);
    }
    }
    else
        printf("Erreur\n\n");
    return 0;
}

void inttolitend(uint32_t x, uint8_t* lit_int){
   lit_int[3] = (uint8_t)x / (256*256*256);
   lit_int[2] = (uint8_t)(x % (256*256*256)) / (256*256);
   lit_int[1] = (uint8_t)((x % (256*256*256)) % (256*256)) / 256;
   lit_int[0] = (uint8_t)((x % (256*256*256)) % (256*256)) % 256;
}

With this I create a disk with 7 block and I write all 7 block in my file. In my first block I wrote the size of my disk (7) in little endian. So block[0] = 00, block[1] = 00, block[2] = 01, block[3] = 11 or something like that.

But when I print my result I get :

0
8
16
24

Not what I expected I try without readand i got the same result. So my programm didn't write the information from the disk. Or there's a problem when I write my block in the disk ?

Jackie
  • 143
  • 1
  • 3
  • 8
  • This is `fwrite(&d->diskBlock[i],sizeof(block),1,f);` still wrong. – Iharob Al Asimi Jan 10 '16 at 15:49
  • @iharob What I have to change ? `sizeof`? – Jackie Jan 10 '16 at 15:50
  • Add the size of the block too, `sizeof(block) + 1024` I think. Please be careful and think it through, I remember from your previous question but I am not 100% sure. – Iharob Al Asimi Jan 10 '16 at 15:52
  • @iharob this is a long story but my university screw this... They didn't teach us the basic of C and run into System stuff with advanced C... So I'm really lost with malloc and all this stuff. – Jackie Jan 10 '16 at 15:55
  • @iharob I changed it and test again. Always the same problem... – Jackie Jan 10 '16 at 15:59
  • How to use a debugger in c ? (Yeah sorry but they really taught nothing in the basic, we only used C to do a personal shell with dirent stuff nothing like that...) – Jackie Jan 10 '16 at 16:03
  • It depends, what compiler are you using? If you want more help write to me iharob@gmail.com – Iharob Al Asimi Jan 10 '16 at 16:04
  • correct this `if((f=open("disk.tfs",O_RDONLY)) == -1)` – milevyo Jan 10 '16 at 16:56
  • First step is to find out is the problem writing the file or reading the file. To look at the first four bytes of your file do od -N 4 -tu1 disk.tfs – John Hascall Jan 10 '16 at 17:35
  • You confusing pointers with data values. First you fill `d->diskBlock[0].unBlock`, then never written it into file, then outputing `(uint8_t)&d->diskBlock[i]` (address of element? why?) and expecting it to be the same as `unBlock`, which it have no reason to be. – keltar Jan 10 '16 at 17:45

1 Answers1

1

i can not be so sure what you want exactly but this should work

#define BLOCK_BUFF_SIZE (1024*sizeof(block))


int tttfs_create(int size, char *name);
int tttfs_load(char *fname, disk *pdisk)

void inttolitend(uint32_t x, uint8_t* lit_int);

int tttfs_load(char *fname, disk *pdisk){
    int fd;
    unsigned int n;
    int i;
    fd = open("disk.tfs",_O_RDONLY);
    if(fd==-1){
        perror("tttfs_load.open");
        return -1;
    }
    n=lseek (fd,0,SEEK_END);
    lseek(fd,0,SEEK_SET);

    if(n==(unsigned int)-1){
        close(fd);
        return -1;
    }

    if(n){
        n/=(BLOCK_BUFF_SIZE);

        pdisk->diskBlock=malloc(n*sizeof(block));

        for(i=0;i<n;i++){
            pdisk->diskBlock[i].unBlock=malloc(BLOCK_BUFF_SIZE);
            read(fd,pdisk->diskBlock[i].unBlock,BLOCK_BUFF_SIZE);
        }

    }
    close(fd);
    return n;
}
int main(){


    unsigned int n;
    disk d;
    int i;

    tttfs_create(7, "disk.tfs");
    n=tttfs_load("disk.tfs",&d);
    if(!n || n==(-1)) return -1;



    for(i=0; i<4; i++){
        printf("%d\n", (uint8_t)(d.diskBlock->unBlock[i]));//print my result.
    }
}

int tttfs_create(int size, char *name){
    FILE    *f = NULL;
    disk    d;
    int     i;

    if (!(f = fopen(name, "wb"))) {
        perror("tttfs_create:open()");
        return 0;
    }


    d.diskBlock = malloc(sizeof(block) * size);
    d.id = 1;

    for(i = 0; i<size; i++){
       d.diskBlock[i].unBlock = malloc(BLOCK_BUFF_SIZE);
    }
    inttolitend(size,  (uint8_t*)(d.diskBlock->unBlock));

    for(i=0;i<size;i++){
        fwrite(d.diskBlock[i].unBlock ,BLOCK_BUFF_SIZE,1,f); 
    }



    return 1;
}

void inttolitend(uint32_t x, uint8_t* lit_int){

    lit_int[3] = ((x<<24) & 0xff);
    lit_int[2] = ((x<<16) & 0xff);
    lit_int[1] = ((x<<8)  & 0xff);
    lit_int[0] = ((x<<0)  & 0xff);

}
milevyo
  • 2,165
  • 1
  • 13
  • 18