c语言文件基础知识

喜你入骨 提交于 2020-01-21 08:35:45

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

/*
* EOF: 符号常量,其值为-1.
* fgets() 最多读取int-1个字符,遇换行或EOF即返回.
* fputs() 写文件时,忽略字符串截止符'\0'.
* fread()和fwrite() 数据块读写,多用于结构体数组(顺序存储的结构体).
*
* 函数原型:
* 读:
* int fgetc(FILE *stream);
* char *fgets(char *s, int size, FILE *stream);
* size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
* int fscanf(FILE *stream, const char *format, ...);
* 写:
* int fputc(int c, FILE *stream);
* int fputs(const char *s, FILE *stream);
* size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
* int fprintf(FILE *stream, const char *format, ...);
* 其他:
* void rewind(FILE *stream); 将文件内部的位置指针重新指向一个流(数据流/文件)的开头.
*
* 打开方式:
* r(read): 读
* w(write): 写
* a(append): 追加
* +: 读和写
* t(text): 文本文件,可省略不写
* b(banary): 二进制文件
*/

FILE *fp = NULL;

// READ
void GetCharFromFile(FILE *fp)
{
int ch = 0;

while ((ch=fgetc(fp)) != EOF) //失败时返回 EOF
{
printf("fget: [%c]\n", ch);
}
}

void GetLineFromFile(FILE *fp)
{
char line[1024] = "";

while (fgets(line, sizeof(line), fp) != NULL) //失败时返回 NULL
{
printf("fgets: [%s]\n", line);
}
}

void GetBlockFromFile(FILE *fp)
{
int i = 0;
int block[10] = {0};
int count = 1;

while (fread(block, sizeof(block), count, fp) == count) //失败时返回值 != count
{
printf("fread: ");
for (i=0; i<10; i++)
{
printf("%d ", block[i]);
}
printf("\n");
}
}

void ReadFormatToFile(FILE *fp)
{
char file[32] = "";
char func[32] = "";
char date[32] = "";
int line = 0;

fscanf(fp, "%s %s %d %[^\n]", file, func, &line, date); //返回读取元素个数,eg:4
printf("file: %s\n", file);
printf("func: %s\n", func);
printf("line: %d\n", line);
printf("date: %s\n", date);
}

// WRITE
int WriteCharToFile(FILE *fp, char ch)
{
int ret = 0;

ret = fputc(ch,fp); //失败时返回EOF
return ret!=EOF?0:-1;
}

int WriteStrToFile(FILE *fp, char *str)
{
int ret = 0;

ret = fputs(str, fp);
return ret!=EOF?0:-1; //失败时返回EOF
}

int WriteBlockToFile(FILE *fp, const void *block, int size, int count)
{
int ret = 0;

ret = fwrite(block, size, count, fp); //失败时返回值 != count
return ret!=count?-1:0;
}

int WriteFormatToFile(FILE *fp)
{
int ret = 0;

ret = fprintf(fp, "%s %s %d %s\n", __FILE__, __func__, __LINE__, __DATE__);
return ret<0?-1:0; //失败时返回一个负值
}

// MAIN
int main(int argc, char **argv)
{
char ch = 'r';
char *str = "Hello World.\n";
int block[10] = {1,2,3,4,5,6,7,8,9,0};
char *filePath = "./ll";

fp = fopen(filePath, "w+"); //不关心文件存在与否,每次重写文件,并可读
if (NULL == fp)
{
perror("fopen");
return -1;
}

// WRITE
// printf("WriteCharToFile: %s\n", WriteCharToFile(fp, ch)?"Fail":"Success");
printf("WriteStrToFile: %s\n", WriteStrToFile(fp, str)?"Fail":"Success");
// printf("WriteBlockToFile: %s\n", WriteBlockToFile(fp, block, sizeof(block), 1)?"Fail":"Success");
// printf("WriteFormatToFile: %s\n", WriteFormatToFile(fp)?"Fail":"Success");

rewind(fp);
// READ
// GetCharFromFile(fp);
GetLineFromFile(fp);
// GetBlockFromFile(fp);
// ReadFormatToFile(fp);

fclose(fp);
return 0;
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!