1、文件打开关闭
打开文件:fopen函数
FILE * fopen(char *filename, char *mode);1、filename:要打开的文件名(路径)2、mode:文件打开模式
-
2、文件读写
fgetc:从文件指针所指向的文件中读取一字节数据,同时将读写位置指针向前移动一字节。
int fgetc(FILE *filepointer);
fputc:将形参c表示的字符数据输出到文件指针filepointer所指向的文件中,同时将读写位置指针向前移动一字节。
int fputc(int c, FILE *filepointer);
读写实例: ```
include
int main(int argc, char argv[]) { FILE fp1, *fp2; int ch; fp1 = fopen(“./test.txt”, “w”); if(!fp1){ return -1; } //写入文件 while (1){ ch = getchar(); if(ch == ‘@’) break; putc(ch, fp1); } fclose(fp1);
fp2 = fopen("./test.txt", "r");if(!fp2){return -1;}//读取文件while ((ch = fgetc(fp2)) != EOF){printf("%c", ch);}fclose(fp2);return 0;
}
<a name="wmrDv"></a>## 3、读写字符串1. fgets、fputs
//从文件中读取n个字节的数据 char fgets(char s, int n, FILE filepointer); //将s的数据传入文件 int fputs(char s, FILE *filepointer);
```#include <stdio.h>#include <stdlib.h>int main(int argc, char *argv[]) {FILE *fp1, *fp2;char str[] = "123456789";char *f1 = "./f1.txt", *f2 = "./f2.txt";fp1 = fopen("f1.txt", "w");if(fp1 == NULL){printf("can not open file :f1.txt\n");exit(0);}fputs(str, fp1);fputs("\nabcd", fp1);fclose(fp1);fp2 = fopen("f1.txt", "rt");fgets(str, 8, fp2);printf("%s\n", str);fgets(str, 8, fp2);printf("%s\n", str);fgets(str, 8, fp2);printf("%s\n", str);fclose(fp2);return 0;}//输出结果123456789abcd
虽然读取的是8个字符,但是第二次遇到了\n,所以实际读取了3个字符。
4、数据块读写fread,fwrite
fread
unsigned fread(void *prt, unsigned size, unsigned n, FILE *filepointer);
从filepointer所指向的文件中读取n个数据项,每个数据项的大小是size字节,这些数据将被存放在ptr指向的内存中。同时,将读写位置指针向前移动n×size字节。
fwrite
unsigned fwrite(void *ptr, unsigned size, unsigned n, FILE *filepointer);
将ptr所指向的内存块数据(n个大小为size字节的数据项)写入到filepointer指向的文件中,实际要写入数据的字节数是n×size。同时将读写位置指针向前移动n×size字节。
示例代码: ```
include
include
include
int main(int argc, char argv[]) { FILE fp; short i, a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
//调用fwrite将数组内容写入文件 //————————————————————————————————- fp = fopen(“f1.dat”, “wb”); if(fp == NULL){ printf(“can not create file: f1.dat\n”); exit(0); } fwrite(a, sizeof(short), 10, fp); fclose(fp);
//调用fread读取二进制文件内容,写入数组
//————————————————————————————————- //数组数据清0 memset(a, 0, 10 * sizeof(short)); fp = fopen(“f1.dat”, “rb”); if(fp == NULL){ printf(“can not open file: f1.dat\n”); exit(0); } fread(a, sizeof(short), 10, fp); fclose(fp);
for(i = 0; i < 10; i++){printf("%d\t", a[i]);}return 0;
}
<a name="pNwkr"></a>## 5、格式化读写函数1. fscanf
int fscanf(FILE filepointer, const char format[, address, …]);
**从filepointer所指向的文件中读取数据**。除了多了一个文件指针的参数外,其它方面和scanf函数完全相同。2. fprintf
int fprintf(FILE filepointer, const char format[, address, …]);
将表达式**输出**到filepointer所指向的文件中。除了多个个文件指针的参数外,其它方面与printf完全相同。3. 示例代码:
include
include
include
typedef struct { char no[9]; char name[10]; char sex; int age; char address[20]; }StudentInfo;
char *filename = “student.dat”;
int main(int argc, char argv[]) { FILE fp; char ch1[20], ch2[20]; fp = fopen(filename, “wt”); if(fp == NULL){ printf(“can not create file: %s\n”, filename); exit(0); }
//输出到文件//------------------------------------------fprintf(fp, "%s %s", "zack", "atom");fclose(fp);fp = fopen(filename, "rt");if(fp == NULL){printf("can not open file: %s\n", filename);exit(0);}//从文件中读取//------------------------------------------fscanf(fp, "%s%s", ch1, ch2);printf("%s, %s", ch1, ch2);return 0;
}
```
