[英]How to write a struct to a file using write() function?
我想使用write()函数将struct对象写入文件。 它必须是那个功能。
我从终端输入的是:./main.c output.dat John Doe 45
当我运行程序并打开output.dat时,会出现一堆没有意义的字母。 请帮我。
我想要的output.dat文件中的输出是:John Doe 45
我的代码:
struct Person{
char* name;
char* lastName;
char* age;
};
int main(int argc, char** argv){
struct Person human;
/* get the argument values and store them into char* */
char* fileName = argv[1];
char* name = argv[2];
char* lastName = argv[3];
char* age = argv[4];
/* set the values of human object */
human.name = name;
human.lastName = lastName;
human.age = age;
/* open the file */
int file = 0;
file = open(fileName, O_RDWR); /* I want to have read&write set! */
write(file, &human, sizeof(human));
close(file);
return 0;
}
编写该结构时,只能在该struct
本身中写入值。 在您的情况下,这些值是指向内存中其他位置的指针,而不是字符串数据。 因此,您最终编写了三个指针,这些指针的内存地址(在大多数系统上为12或24字节)并不是全部有用(因为它们适用于当前正在运行的程序的内存空间)下一次运行)。
您将需要设计一种更有用的序列化格式,该格式实际上写出字符串的内容 ,而不是它们的地址。 选项包括简单的换行符或NUL
分隔的文本,二进制长度的前缀文本,或带有第三方库的CSV,JSON或XML(如果您有野心,请使用某种数据库)。
例如,使用二进制长度的前缀文本,您可以执行以下操作:
uint32_t len;
len = strlen(name);
write(file, &len, sizeof(len));
write(file, human.name, len);
len = strlen(lastName);
write(file, &len, sizeof(len));
write(file, human.lastName, len);
... repeat for age ...
它允许您通过读取每个字符串长度(固定大小)来重新读回它,然后使用它来计算必须读取多少字节才能获得该字符串。
您不能只写出对象。 您需要分别写出每个内部指针。
像这样:
file = open(fileName, O_RDWR); /* I want to have read&write set! */
write(file, human.name, std::strlen(human.name) + 1);
write(file, human.lastName, std::strlen(human.lastName) + 1);
write(file, human.age, std::strlen(human.age) + 1);
close(file);
注意,我在字符串的长度上添加了+1
,以确保我也写出了终止的零。
如果您知道每个字段的最大长度,则可以尝试使这些字段成为数组。 记住为空字节加1
struct Person{
char name[32]; //31 char long + null
char lastName[32]; // 31 char long + null
char age[4]; // 3 char long + null
};
然后,您的fwrite将正常工作。 但是您需要将值存储在结构中。
strlcpy(human.name, name, sizeof(human.name));
对于每个字段,依此类推。 strlcpy确保您的字符串以null终止。
多亏了所有人,我才这样喜欢它。 虽然不理想,但可以完成工作:):
struct Person{
char name[20];
char lastName[20];
char age[20];
};
int main(int argc, char** argv){
struct Person human;
/* get the argument values and store them into char* */
char* fileName = argv[1];
char* name = argv[2];
char* lastName = argv[3];
char* age = argv[4];
sprintf(human.name,name);
sprintf(human.lastName,lastName);
sprintf(human.age,age);
/* open the file */
int file = 0;
file = open(fileName, O_RDWR); /* I want to have read&write set! */
write(file, &human, sizeof(human));
close(file);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.