C语言的文件函数
stream表示流
1:fputs
int fputs(int character,FILE* stream);
#include<stdio.h>
int main()
{
FILE* pf=fopen("data.txt", "w");
//打开判断是否成功
if (pf == NULL)
{
perror("fopen");
return 1;
}
fputc('a', pf);
fputc('a', pf);
fputc('a', pf);
fputc('a', pf);
fputc('a', pf);
//关闭
fclose(pf);
pf == NULL;
return 0;
}
fputc的第二个是字符,不是字符串。
2:fgetc
int getc(FILE* stream);
使用int类型来接收返回值,是因为如果返回错误就算返回-1(EOF)
int main()
{
FILE* pf = fopen("data.txt", "r");
//打开判断是否成功
if (pf == NULL)
{
perror("fopen");
return 1;
}
int ch = fgetc(pf);
printf("%c\n",ch);
//关闭
fclose(pf);
pf == NULL;
return 0;
}
ch表示一次接受一个字符
3:fputs
int fputs(const char* str, FILE * stream);
int main()
{
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
fputs("i ilke c", pf);
fclose(pf);
pf = NULL;
return 0;
}
4:fgets
char* fgets(char* str, int num, FILE * stream);
int main()
{
char str[100];
fgets(str,100,stdin);
printf("%s", str);
return 0;
}
5:fprintf
int fprintf(FILE * stream, const char* format, ...);
int main()
{
struct st
{
int age;
char name[100];
char sex[10];
};
struct st s = { 10,"xiaogang","boy" };
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
perror("fopen");
return 1;
}
fprintf(pf, "%d %s %s", s.age, s.name, s.sex);
return 0;
}
6:fscanf
int fscanf(FILE * stream, const char* format, ...);
int main()
{
FILE* pf = fopen("data.txt","r");
if (pf == NULL)
{
perror("fopen");
return 1;
}
int age;
char name[100];
char sex[100];
fscanf(pf, "%d %s %s", &age, name, sex);
printf("%d %s %s", age, name, sex);
return 0;
}
7:fread和fwrite(二进制输入和输出)
ferad: size_t fread(void* ptr, size_t size, size_t count, FILE * stream);
fwrite: size_t fwrite(const void* ptr, size_t size, size_t count, FILE * stream);
int main()
{
//fwrite
FILE* pf = fopen("data.txt", "wb");
if (pf == NULL)
{
perror("fopen");
return 1;
}
int arr[] = { 1,2,3,4,5,6,7,8,9 };
fwrite(arr,sizeof(int),10,pf);
fclose(pf);
pf = NULL;
//fread
FILE* pf2 = fopen("data.txt", "rb");
if (pf2 == NULL)
{
perror("fopen");
return 1;
}
int arr2[10] = { 0 };
fread(arr2, sizeof(int), 10, pf2);
for (int i = 0; i < 9; i++)
{
printf("%d ",arr2[i]);
}
fclose(pf2);
pf2 = NULL;
return 0;
}
8:fseek
根据⽂件指针的位置和偏移量来定位⽂件指针(⽂件内容的光标)。
int fseek ( FILE * stream, long int offset, int origin );
int main()
{
FILE* pFile;
pFile = fopen("example.txt", "wb");
fputs("This is an apple.", pFile);
fseek(pFile, 9, SEEK_SET);
fputs(" sam", pFile);
fclose(pFile);
return 0;
}
9:ftell
返回⽂件指针相对于起始位置的偏移量
long int ftell ( FILE * stream );
#include <stdio.h>
int main ()
{
FILE * pFile;
long size;
pFile = fopen ("myfile.txt","rb");
if (pFile==NULL)
perror ("Error opening file");
else
{
fseek (pFile, 0, SEEK_END); // non-portable
size=ftell (pFile);
fclose (pFile);
printf ("Size of myfile.txt: %ld bytes.\n",size);
}
return 0;
}
10:rewind
让文件指针位置回到起始位置
int main()
{
FILE* pFile;
long size;
pFile = fopen("example.txt", "rb");
if (pFile == NULL)
perror("Error opening file");
else
{
fseek(pFile, 0, SEEK_END); // non-portable
size = ftell(pFile);
printf("Size of example.txt: %ld bytes.\n", size);
rewind(pFile);
fseek(pFile, 0, SEEK_END); // non-portable
size = ftell(pFile);
fclose(pFile);
printf("Size of example.txt: %ld bytes.\n", size);
}
return 0;
}
11:feof(只能检测是否遇到文件末尾)
牢记:在⽂件读取过程中,不能⽤feof函数的返回值直接来判断⽂件的是否结束。
feof
的作⽤是:当⽂件读取结束的时候,判断是读取结束的原因是否是:遇到⽂件尾结束。
⽂本⽂件读取是否结束,判断返回值是否为
EOF
(
fgetc
),或者
NULL
(
fgets
)
例如:
•
fgetc
判断是否为
EOF
.
•
fgets
判断返回值是否为
NULL
.
二进制⽂件的读取结束判断,判断返回值是否⼩于实际要读的个数。
例如:
•
fread判断返回值是否⼩于实际要读的个数。