输出结构体中所有成员变量的值到文件

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

 

输出结构体中,所有成员变量的值到文件之中的小例子 

#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



 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值