#include
void test1()
{
/*
w:(1)文件存在,將文件清空,再進行寫,(2)文件不存在,新建文件
r:(1)文件存在,正常打開(2)文件不存在,打開失敗
*/
/**
*? 1.打開文件
*? @param restrict#> 文件路徑 description#>
*? @param restrict#> 文件打開的方式 description#>
*? @return
*/
FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "w");
if(fp==NULL)
{
printf("打開失敗\n");
return ;
}
//2.讀或寫操作
/**? *? fputc:是將一個字符寫入到文件里,
*/
char ch='b';
int res=fputc(ch, fp);
printf("res=%d\n",res);
//3.關閉文件
fclose(fp);
}
void test2()
{? //1.打開文件
FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "r");
if(NULL==fp)
{
perror("fopen failed");
return ;
}
//2.對文件操作:
fgetc:意為從文件中讀取一個字符,讀取一個字節后,光標位置后移一個字節。
int ch=fgetc(fp);
printf("第一次讀ch=%c\n",ch);
ch=fgetc(fp);
printf("第二次讀ch=%d\n",ch);
printf("EOF=%d\n",EOF);
//3.關閉文件? ? fclose(fp);
}
void test3()
{
/*
拷貝文件4.txt里面的內容到3.txt里面。用fputc,fgetc實現
*/
}
void test4()
{
//1.fprintf:fprintf( )會根據參數format 字符串來轉換并格式化數據, 然后將結果輸出到參數stream 指定的文件中, 直到出現字符串結束('\0')為止。
FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "w");
if(fp==NULL)
{
perror("fopen failed:");
return ;
}
//2.
fprintf(fp, "a=%d;;;;;b=%d\n",10,5);
//3.
fclose(fp);
}
void test5()
{
//1.
FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "r");
if(fp==NULL)
{
perror("fopen failed:");
return ;
}? ? //2.
int a,b;
fscanf(fp, "a=%d;;;;;b=%d\n",&a,&b);//fscanf:其功能為根據數據格式(format)從輸入流(stream)中寫入數據(argument);與fgets的差別在于:fscanf遇到空格和換行時結束,注意空格時也結束,fgets遇到空格不結束。
printf("a=%d,b=%d\n",a,b);
//3.
fclose(fp);
}
void test6(){
//1.
FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "a+");
if(fp==NULL)
{
perror("fopen failed:");
return ;
}
//unsigned char *_p
/*? ? ftell:告訴你文件指針相對偏移量的位置
*/
long position=ftell(fp);
printf("position=%ld\n",position);
//2.
fputc('a', fp);
position=ftell(fp);
printf("position=%ld\n",position);
fputc('b', fp);
position=ftell(fp);
printf("position=%ld\n",position);
fclose(fp);
}
void test7(){
//1.
FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "w");
if(fp==NULL)
{
perror("fopen failed:");
return ;
}
//2.
//fputs:將字符串寫進文件里。
fputs("wo men ban hen niu bi\nxiao wai sheng hen cai\n", fp);
//3.? ? fclose(fp);
}
void test8()
{
//1.
FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "r");
//2.
/*
1>將內容保存指定的變量里,? ? 2>指定多大個字節接收,
3>指定文件
*/
/*
fgets():讀取一行,碰到\n,表示讀取完成
*/
char string[50];
fgets(string, 50, fp);
printf("string=%s",string);
fgets(string, 50, fp);
printf("string=%s",string);? ? //3.
fclose(fp);
}
void test9()
{
int arr[5]={0,2,3,4,5};
FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "w");
/*
參數解釋
1>存儲變量的地址
2>變量多大
3>存儲這種類型變量的個數
4>存儲到哪個文件
fwrite:
size_t fwrite(const void* buffer, size_t size, size_t count, FILE* stream);
注意:這個函數以二進制形式對文件進行操作,不局限于文本文件
返回值:返回實際寫入的數據塊數目
(1)buffer:是一個指針,對fwrite來說,是要獲取數據的地址;
(2)size:要寫入內容的單字節數;
(3)count:要進行寫入size字節的數據項的個數;
(4)stream:目標文件指針;
(5)返回實際寫入的數據項個數count。
說明:寫入到文件的哪里? 這個與文件的打開模式有關,如果是w+,則是從file pointer指向的地址開始寫,替換掉之后的內容,文件的長度可以不變,stream的位置移動count個數;如果是a+,則從文件的末尾開始添加,文件長度加大。
fseek對此函數有作用,但是fwrite[1]函數寫到用戶空間緩沖區,并未同步到文件中,所以修改后要將內存與文件同步可以用fflush(FILE *fp)函數同步。
*/? ? fwrite(arr, sizeof(int), 5, fp);
fclose(fp);
}
void test10()
{
//1.
FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "r");
//2.? ? /*
參數解釋
1>將讀到的內容存到哪個變量里,指定變量的地址
2>變量多大
3>讀取這種類型變量的個數
4>讀取哪個文件
fread:
fread是一個函數。從一個文件流中讀數據,最多讀取count個項,每個項size個字節,如果調用成功返回實際讀取到的項個數(小于或等于count),如果不成功或讀到文件末尾返回 0。
*/
int brr[5];
fread(brr, sizeof(int), 5, fp);
int i=0;
for (; i<5; i++)
{
printf("brr[%d]=%d\n",i,brr[i]);
}
fclose(fp);
}
typedef struct Student
{
char sex;
int age;
}Student;
void test11()
{
Student stu={'x',20};
//1.
FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "w");
//2.
fwrite(&stu, sizeof(stu), 1, fp);
//3.
fclose(fp);
}
void test12()
{
Student stu;
//1.? ? FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "r");
//2.
fread(&stu, sizeof(stu), 1, fp);
printf("age=%d,sex=%c\n",stu.age,stu.sex);
//3.
fclose(fp);
}
void test13()
{
Student stu[3]={{'x',20},{'m',30},{'b',40}};
//1.? ? FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "w");
//2.
int i=0;
for (; i<3; i++)
{
fwrite(&stu[i], sizeof(Student), 1, fp);
}
//3.
fclose(fp);
}
void test14()
{
Student stu[3];
//1.? ? FILE *fp=fopen("/Users/ccj/Desktop/2.txt", "r");
//2.
fread(stu, sizeof(stu), 1, fp);
int i=0;
for (; i<3; i++)
{
printf("stu[%d]的age=%d,sex=%c\n",i,stu[i].age,stu[i].sex);
}
//3.
fclose(fp);
}
int main()
{
test14();
return 0;
}