从文件中读取结构

gsa*_*ras -1 c linux io posix file

我试图在一个文件中写一个4个无符号整数的结构,然后再读回来,但我什么都看不懂.

这是我的代码:

// 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);
}
Run Code Online (Sandbox Code Playgroud)

它甚至不会进入循环.我错过了什么?

dav*_*mac 5

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