fopen函数“const char *mode”参数选项。
结果:
标准库c写入结构体到文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
struct Test
{
int a;
char c;
};
int main()
{
FILE *fp;
struct Test data[2] = {{100,'a'},{101,'b'}};
struct Test data2[2];
fp = fopen("./file1","w+");
int n_write = fwrite(&data,sizeof(struct Test)*2,1,fp);
fseek(fp,0,SEEK_SET);
int n_read = fread(&data2,sizeof(struct Test)*2,1,fp);
printf("fread %d,%c\n",data2[0].a,data2[0].c);
printf("fread %d,%c\n",data2[1].a,data2[1].c);
fclose(fp);
return 0;
}
fputc()函数运用
#include <stdio.h>
#include <string.h>
main()
{
FILE *fp;
int i;
char *str = "win!!!";
int len = strlen(str);
fp = fopen("./test.txt","w+");
for(i=0;i<len;i++){
// int fputc(int c, FILE *stream);
fputc(*str,fp);
str++;
}
fclose(fp);
return 0;
}
结果: