输出结构体中,所有成员变量的值到文件之中的小例子
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct st{
float f;
char * a;
char * b;
char * c;
};
struct st st_var;
int main(){
st_var.f = 0.0382;
st_var.a = "a";
st_var.b = "b";
st_var.c = "c";
char s[128];
int fd = open("output.log", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR );
snprintf(s, sizeof(s), "%f %s %s %s\n", st_var.f, st_var.a, st_var.b, st_var.c);
write(fd, s, strlen(s));
close(fd);
}
结果:
0.038200 a b c

本文提供了一个简单的C程序示例,演示如何将结构体中的成员变量输出到文件中。

1万+

被折叠的 条评论
为什么被折叠?



