简体   繁体   English

从文件读取结构

[英]Read a struct from file

I am trying to write a struct of 4 unsigned integers in a file and then read it back, but I can not read anything. 我正在尝试在文件中写入4个无符号整数的结构,然后将其读回,但是我什么也看不到。

Here is my code: 这是我的代码:

// create and write values in superblock
..
Superblock s; // the struct
if(write(file_desc , &superblock , sizeof(Superblock)) == -1) {
  perror("writing superblock");
  exit(1);
}
close(file_desc);
file_desc = open(path_file, O_WRONLY | O_CREAT, 0600);
while ( ( read( file_desc , &s , sizeof(Superblock))) > 0 ) {
  printf("%u %u %u %u\n", s.block_size, s.filename_size, s.max_file_size,
         s.max_dir_file_no);
}

It will not even enter the loop. 它甚至不会进入循环。 What am I missing? 我想念什么?

您正在打开只写文件( O_WRONLY ),然后尝试从中read

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM