-1

嗨,我想读取我在文件中写入的结构中的一些信息,fwrite但是有一个问题我无法提取这些信息。我有2个文件

ttfs.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;
}

有了这个,我创建了一个包含 7 个块的磁盘,并将所有 7 个块写入我的文件中。在我的第一个块中,我用小端写了我的磁盘大小(7)。所以 block[0] = 00, block[1] = 00, block[2] = 01, block[3] = 11 或类似的东西。

但是当我打印我的结果时,我得到:

0
8
16
24

不是我所期望的,我没有尝试read,我得到了相同的结果。所以我的程序没有从磁盘写入信息。或者当我在磁盘中写入我的块时出现问题?

4

1 回答 1

1

我不能确定你到底想要什么,但这应该可以

#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);

}
于 2016-01-10T19:20:00.207 回答