linux下C 大家帮我看看代码错哪里了 为什么结构体里的内容不能写到文件中呢
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct syster
{
char SysID[5];
char SysName[19];
char SubSysID[19];
char SubSysName[22];
int NodeID;
char NodeUpTime[30];
char NodeDescr[20];
}sys={"k1","Host","009","HostMonitor",2,"2005-04-01","node"};
char buf[100];
int WriteStruFile(char *ptrFileName, struct syster *ptrStr)
{
int fd;
int length;
int res;
memset(buf,0,sizeof(buf));
sprintf(buf,"[SysID]=%s\n",ptrStr->SysID);
printf("%s\n",buf);
if(fd = open(ptrFileName,O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE) == -1)
{
printf("open file failed!!");
return -1;
}
length = strlen(buf);
if ((res = write(fd, buf, length)) != length)
{
printf("Error writing to the file.\n");
exit(1);
}
close(fd);
}
int main()
{
WriteStruFile("/root/changye/wwww/SysTable",&sys);
return 0;
}
...全文
相关推荐
linux C语言 使用结构体对文件操作 读 写 查找
liunx下面的对文件的操作,使用了结构体的操作,代码均调试成功,在必要的地方还加上注释了,欢迎大家下载,如果有什么问题,欢迎提出,我们大家一起进步哦。
linux c之memcpy拷贝结构体到结构体、拷贝字符数组到结构体
1 memcpy 我们知道这个函数主要是拷贝内存数据,我们一般可以使用拷贝结构体到结构体、也可以拷贝字符数组到结构体,但是这个字符数组数据应该是同一个结构体拷贝先拷贝到这个字符数组,如果是其他格式的字符数组,这样拷贝就有问题。 2测试代码 #include <stdio.h> #include <string.h> typedef ...
Linux实现目录和文件拷贝C代码
通过书籍和查资料写的程序,自己上机调试运行过,代码中添加注释。
Java 解析 Linux C结构体
Linux C socket通信传输结构体消息,Java解析C语言结构体里的数据
LINUX下c语言读取文件信息
c语言读取文件信息 c语言读取文件信息 LINUX LINUX


