70,023
社区成员




#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;
}
Node n;
.....
FILE* fp;
fp=fopen("1.dat","wb");
if(fp!=NULL)
{
fwrite(&n,sizeof(Node),1,fp);
}
fclose(fp);
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.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);
fd = open(ptrFileName,O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
length = strlen(buf);
res = write(fd, buf, length);
close(fd);
}
int main()
{
WriteStruFile("/home/whf/aaa",&sys);//这个文件名是我自己的
return 0;
}