一、fputs函数
#include <stdio.h>
//fputs函数
int main(int argc,char *argv[])
{
FILE *fp;
char writeBuff[] = "hello world";
fp = fopen("a.c","w+");
if(fp == NULL)//判断文件是否打开
{
printf("open file failed\n");
return -1;
}
fputs(writeBuff,fp);//把文件内容写入数组并打印出来
fclose(fp);
return 0;
}
二、fgets函数
#include <stdio.h>
//fgets函数
int main(int argc,char *argv[])
{
FILE *fp;
char writeBuff[] = "hello world";
char readBuff[128] = {0};
fp = fopen("a.c","w+");
if(fp == NULL)
{
printf("open file failed\n");
return -1;
}
fputs(writeBuff,fp);
fseek(fp,0,SEEK_SET);//偏移量为0,光标在文件内容开头
fgets(readBuff,128,fp);//读取数组数据
printf("readBuff is %s\n",readBuff);//打印读取内容
fclose(fp);
return 0;
}