fputs,fgets:
/*
\r\n windows下存入文本数据时换行符会变成/r/n(/r是换行/n是将光标移动到行首) \nLinux下正常
int fputs( const char *str, FILE *stream );
标准解释:
fputs()函数把str(字符串)指向的字符写到给出的输出流. 成功时返回非负值, 失败时返回EOF.
返回值:
成功返回0
失败返回-1
自己的理解:
写文件是把一个字符串写进文件中,以\0为基础,直到写到\0,重点:*******\0不会写到文件中**************
char *fgets( char *str, int num, FILE *stream );
标准解释:
函数fgets()从给出的文件流中读取[num - 1]个字符并且把它们转储到str(字符串)中.
fgets()在到达行末时停止,在这种情况下,str(字符串)将会被一个新行符结束.
如果fgets()达到[num - 1]个字符或者遇到EOF, str(字符串)将会以null结束.fgets()成功时返回str(字符串),失败时返回NULL.
返回值:
成功:成功读取的字符串
读到文件尾或出错: NULL
自己的理解:
fgets 是读取一行,如果num>这一行的字符个数,则将这一行字符全部读出
若num<=这一行字符的个数,那么会读取num-1个字符,最后一个字符写\0
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *fileName = "./b.txt";
void linesWrite() //行写文件
{
FILE *fp = fopen(fileName, "w");
if (fp == NULL)
{
return;
}
char *str[] = {
"01_hello world!\n",
"02_hello world!\n",
"03_hello world!\n",
"04_hello world!\n",
"05_hello world!\n"
};
int len = sizeof(str) / sizeof(char *);
for (int i = 0; i < len; i++)
{
fputs(str[i], fp);
}
fclose(fp);
}
void lineRead()//行读文件
{
FILE *fp = fopen(fileName, "r");
if (fp == NULL)
{
return;
}
char buf[1024] = { 0 };
while (!feof(fp))
{
if (feof(fp))//如果到文件末尾则退出
{
break;
}
fgets(buf, 1024, fp);
printf("%s", buf);
memset(buf, 0, 1024);
}
fclose(fp);
}
void main()
{
linesWrite();
lineRead();
system("pause");
}
fread fwrite:
/*
块读写文件,读写的是一片内存,将内存里的数据全部写入或读出
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:以数据块的方式给文件写入内容
参数:
ptr:准备写入文件数据的地址
size: size_t 为 unsigned int类型,此参数指定写入文件内容的块数据大小
nmemb:写入文件的块数,写入文件数据总大小为:size * nmemb
stream:已经打开的文件指针
返回值:
成功:实际成功写入文件数据的块数,此值和nmemb相等
失败:0
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:以数据块的方式从文件中读取内容
参数:
ptr:存放读取出来数据的内存空间
size: size_t 为 unsigned int类型,此参数指定读取文件内容的块数据大小
nmemb:读取文件的块数,读取文件数据总大小为:size * nmemb
stream:已经打开的文件指针
返回值:
成功:实际成功读取到内容的块数,如果此值比nmemb小,但大于0,说明读到文件的结尾。
失败:0
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct Person
{
char name[64];
int age;
}Person;
const char *fileName = "./a.txt";
void test01()
{
FILE *fp = fopen(fileName, "wb");
if (fp == NULL)
{
return;
}
Person p1 = { "Obama", 23 };
fwrite(&p1, sizeof(Person), 1, fp);
fclose(fp);
fp = NULL;
fp = fopen(fileName, "rb");
if (fp == NULL)
{
return;
}
Person p2;
fread(&p2, sizeof(Person), 1, fp);
printf("name:%s\nage:%d\n", p2.name, p2.age);
fclose(fp);
}
void test02()//写数组进文件
{
FILE *fp = fopen(fileName, "wb");
if (fp == NULL)
{
return;
}
Person p[] = {
{ "Obama",23 },
{ "Mary", 25 },
{ "chuishi", 213 },
{ "zhangchuanxu", 21 }
};
int count = sizeof(p) / sizeof(Person);
fwrite(&p, sizeof(Person), count, fp);
fclose(fp);
fp = NULL;
fp = fopen(fileName, "rb");
if (fp == NULL)
{
return;
}
Person temp;
memset(&temp, 0, sizeof(Person));
for (int i = 0; i < count; i++)
{
fread(&temp, sizeof(Person), 1, fp);
printf("names:%s\nage:%d\n", temp.name, temp.age);
memset(&temp, 0, sizeof(Person));
}
fclose(fp);
}
void test03()//随机读取某个
{
FILE *fp = fopen(fileName, "rb");
if (fp == NULL)
{
return;
}
Person temp;
memset(&temp, 0, sizeof(Person));
fseek(fp, sizeof(Person), SEEK_SET);
fread(&temp, sizeof(Person), 1, fp);
printf("names:%s\nage:%d\n", temp.name, temp.age);
fclose(fp);
}
void main()
{
//test01();
//test02();
test03();
system("pause");
}
fgetc fputc:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *fileName = "./a.txt"; //相对路径 /******** 路径中的斜杠要用45度的/ 尽量不要用\,45度/在Windows和Linux下通用 ***********/
//const char *fileName = "C:/Users/传旭/Documents/Visual Studio 2013/Projects/161013_文件按字符读写/161013_文件按字符读写/a.txt" //绝对路径
void writeFile()
{
FILE *file_write = fopen(fileName, "w");
if (file_write == NULL)
{
return;
}
char *data = "hello world!\nhello Obma!\n\0";
int len = strlen(data);
for (int i = 0; i < len; i++)
{
fputc(data[i], file_write);
}
fclose(file_write);
}
void readFile()
{
FILE *file_read = fopen(fileName, "r");
if (file_read == NULL)
{
return ;
}
#if 0
char ch;
while ((ch = fgetc(file_read)) != EOF)//EOF 是文件的末尾
{
putchar(ch);
}
#else
//函数feof()在到达给出的文件流的文件尾时返回一个非零值.
//hello world!\nhello Obma!\nEOF
// A
while (!feof(file_read))//feof函数判断是否到文件末尾
{
char ch = fgetc(file_read);
if (ch == EOF) //此处要注意的是当文件指针指向EOF以后(标志位flag 才会变化)此时仍要执行获取输出
{
break; //不加此判断语句就会多输出空格
}
putchar(ch);
}
#endif
fclose(file_read);
}
void fileRead(FILE *fp, char *buf)
{
if (fp == NULL || buf == NULL)
{
return;
}
char ch;
int count = 0;
while ((ch = fgetc(fp)) != EOF)
{
buf[count++] = ch;
}
}
void test()
{
FILE *fp = fopen(fileName, "r");
if (fp == NULL)
{
return;
}
char buf[1024] = { 0 };
fileRead(fp, buf);//此处指针已经指到文件末尾
//printf("%s", buf);
char buf1[1024] = { 0 };
//rewind(fp); //将指针重新指向文件头部
fseek(fp, 0, SEEK_SET); //int fseek( FILE *stream, long offset, int origin );
//fseek 中offset为偏移量,为1则表示指针向右边移动1个偏移量
//int origin 可三选一 SEEK_SET:从文件的开始处开始搜索 SEEK_CUR:从当前位置开始搜索 SEEK_END:从文件的结束处开始搜索
fileRead(fp, buf1);
printf("%s", buf1);
fclose(fp);
}
void main()
{
writeFile();
readFile();
//test();
system("pause");
}