English 中文(简体)
FREAD 核心问题
原标题:FREAD C pointer problem

Hi i have a really frustrating problem with pointers could somebody let me know what I am doing incorrectly here. Thanks

// This functions reads from the file 
void get__data_block_from_disk(char* ptr, int block_num){
    int file_desc;
    int x;
    open_fs(file_path);
    file_desc = fileno(fileptr);
    x = lseek(file_desc, DATA_BLOCK_OFFSET + block_num*BLOCK_SIZE, SEEK_SET);
    fread(&ptr, BLOCK_SIZE, 1, fileptr);
    close_fs();
}

// This function writes to the file
void place__data_block_into_disk(char* ptr, int block_num){
    int file_desc;
    int x;
    printf("char in place: %c
", ptr);
    open_fs(file_path);
    file_desc = fileno(fileptr);
    x = lseek(file_desc, DATA_BLOCK_OFFSET + block_num*BLOCK_SIZE, SEEK_SET);
    fwrite(ptr, BLOCK_SIZE, 1, fileptr);
    close_fs();
}
最佳回答

问题是你写的

fread(&ptr, BLOCK_SIZE, 1, fileptr);

您打算:

fread(ptr, BLOCK_SIZE, 1, fileptr);

问题是,<代码>ptr是向get__data_block_ from_disk提供的。 achar *。 因此,我假定<代码>ptr各点用于接收结果的记忆。 (我希望你已经指出:enough<>/em>,但你没有提供本案呼吁书清单。)

鉴于ptr已经是你希望读到数据时的记忆器,这就是你提供的<代码><>fread。 通过提供<代码>ptr的地址,通过写作&ptr,你正在建议<代码>fread(>,以压倒储存<编码>ptr<>>>>和任何相邻变量的记忆。 毫无疑问,这不是你打算做的事。

如果你误用C++汇编者汇编你的C码,请停办。 但是,如果你知道,C++汇编者将抱怨缺乏<代码>reinterpret_cast<> 载于ptr>,从char *, 避免*

问题回答

页: 1 如果在档案记录员上使用 l,则在档案记录员上读取。 如果您在FLE*上使用read,请在FLE上使用 f。 两者的混合几乎肯定不会给你想要的东西。

fread(&ptr, BLOCK_SIZE, 1, fileptr); 

Should be

fread(ptr, BLOCK_SIZE, 1, fileptr);

or instead you change

void get__data_block_from_disk(char* ptr, int block_num){

to

void get__data_block_from_disk(char** ptr, int block_num){

and

fread(&ptr, BLOCK_SIZE, 1, fileptr); 

to

fread(*ptr, BLOCK_SIZE, 1, fileptr);

Explanation:

Fread first parameter should be a pointer and when you write &ptr you send in the pointers memory adress.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签