我需要将运行期间生成的数据保存在文件中以供以后分析。
xv6 实现了这种原始形式,我不知道它是如何工作的......
是否有捷径可寻?
在用户模式下,您可以这样做:
#include "types.h"
#include "user.h"
#include "fcntl.h"
#define N 100
struct test {
char name;
int number;
};
void
save(void)
{
int fd;
struct test t;
t.name = 's';
t.number = 1;
fd = open("backup", O_CREATE | O_RDWR);
if(fd >= 0) {
printf(1, "ok: create backup file succeed\n");
} else {
printf(1, "error: create backup file failed\n");
exit();
}
int size = sizeof(t);
if(write(fd, &t, size) != size){
printf(1, "error: write to backup file failed\n");
exit();
}
printf(1, "write ok\n");
close(fd);
}
void
load(void)
{
int fd;
struct test t;
fd = open("backup", O_RDONLY);
if(fd >= 0) {
printf(1, "ok: read backup file succeed\n");
} else {
printf(1, "error: read backup file failed\n");
exit();
}
int size = sizeof(t);
if(read(fd, &t, size) != size){
printf(1, "error: read from backup file failed\n");
exit();
}
printf(1, "file contents name %c and number %d", t.name, t.number);
printf(1, "read ok\n");
close(fd);
}
int
main(void)
{
save();
load();
exit();
}