文件输入/输出

来源:互联网 发布:mac 显卡驱动 编辑:程序博客网 时间:2024/05/19 16:28

1.fopen()函数

fopen()函数用来打开一个文件,其函数原型为:FILE *fopen( const char *filename, const char *mode );filename为文件名,mode为文件的操作类型,即fopen()函数的模式字符串。若文件打开成功,则函数的返回值为指向文件类型的指针,否则,返回NULL。
fopen()函数的模式字符串如下:
“r”:打开一个文本文件,可以读取文件;
“w”:打开一个文本文件,可以写入文件,先将文件的长度截为零。如果该文件不存在则先创建该文件;
“a”:打开一个文本文件,可以写入文件,向已有的文件的尾部追加内容。如果该文件不存在则先创建该文件;
“r+”:打开一个文本文件,可以进行更新,即可以读取和写入文件;
“w+”:打开一个文本文件,可以进行更新,即可以读取和写入文件,如果该文件存在则先将其长度截为零,如果不存在则先创建该文件;
“a+”:打开一个文本文件,可以进行更新,即可以读取和写入文件,向已有文件的尾部追加内容,如果该文件不存在则先创建该文件。

2.fclose()函数

fclose()函数关闭由指针指定的文件,同时根据需要刷新缓冲区。其函数原型为:int fclose( FILE *stream );如果文件成功关闭,函数将返回值0,否则返回EOF(符号常量,其值为-1)。

3.fputc()函数和fputs()函数

fputc()函数原型为:int fputc( int c, FILE *stream );函数将参数c转换为unsigned char类型,然后写入参数stream指定的文件中。如果字符写入成功,则函数返回该字符,即参数c,若写入失败,则返回EOF。
fputs()函数原型为:int fputs( const char *string, FILE *stream );fputs()函数与fputc()函数不同的是fputs()函数向文件写入的是一个字符串(不自动写入字符串结束标识符’\0’)。成功写入一个字符串后,文件的位置指针会自动后移,函数返回值为非负整数;否则返回EOF。
下面来运用以上四个函数。

#include<stdio.h>#include<stdlib.h>int main(){    FILE *pfwrite = fopen("text.txt", "w");    if (pfwrite == NULL)    {        perror("error:");        exit(EXIT_FAILURE);    }    //fputc('a', pfwrite);//将字符a写入文本文件text中    fputs("abcde", pfwrite);//将字符串abcde写入文本文件text中    fclose(pfwrite);    system("pause");    return 0;}

4.fgetc()函数和fgets()函数

fgetc()函数原型为:int fgetc( FILE *stream );从文件指针stream指向的文件中读取一个字符,读取一个字节后,光标位置后移一个字节,即如果再次调用该函数,则读取下一个字节。若读取成功,则返回读取的一个字节,若读到文件尾部或者读取错误则返回EOF。
fgets()函数原型为:char *fgets( char *buf, int bufsize, FILE *stream );buf为指向字符型的指针,用来存储所得数据的地址,bufsize为整型数据,指明存储数据的大小,stream为指向文件类型的指针,即将要读取的文件流。若读取成功,则返回第一个参数buf;若读取到文件尾部,则buf保持原来的内容,返回NULL;若读取错误,则buf的内容可能被改变,返回NULL。fgets()函数和fgetc()函数的区别是fgets()函数读取的是字符串。
下面我们来运用这两个函数。

//fgetc()函数#include<stdio.h>#include<stdlib.h>int main(){    FILE *pfread = fopen("text.txt", "r");//假设已有文本文件text,内容为abcde    int ch = 0;//用来接收fgetc()的返回值    if (pfread == NULL)    {        perror("error:");        exit(EXIT_FAILURE);    }    ch=fgetc(pfread);//读取到字符a    putchar(ch);    ch = fgetc(pfread);//读取到字符b    putchar(ch);    fclose(pfread);    system("pause");    return 0;}
//fgets()函数#include<stdio.h>#include<stdlib.h>int main(){    FILE *pfread = fopen("text.txt", "r");//假设已有文本文件text,内容为abcde    char buf[20] = { 0 };    if (pfread == NULL)    {        perror("error:");        exit(EXIT_FAILURE);    }    fgets(buf, 10, pfread);    //将从文件指针pfread指向的文件中读取到的字符串的首地址存入buf中,遇到字符串结束标志或者达到最大读取字节10时则停止读取。    printf("%s\n", buf);//显示abcde    system("pause");    return 0;}

5.fprintf()函数和fscanf()函数

文件I/O函数fprintf()和fscanf()的工作方式与printf()和scanf()相似,区别在于前者第一个参数来指定合适的文件。
fprintf()函数的作用是格式化输出数据到一个流/文件中(printf()函数输出数据到显示屏上),其函数原型为:int fprintf( FILE *stream, const char *format [, argument ]…);fprintf()函数根据指定的格式format向流stream指定的文件中写入数据。函数的返回值为输出的字符数,发生错误是返回一个负值。
fscanf()函数的作用是格式化从输入流中写入数据,其函数原型为:int fscanf( FILE *stream, const char *format [, argument ]… ); fscanf()遇到空格和换行时结束。

//fprintf()函数#include<stdio.h>#include<stdlib.h>int main(){    FILE *pf = fopen("text.txt", "w");    if (pf == NULL)    {        perror("error:");        exit(EXIT_FAILURE);    }    fprintf(pf, "%s %d %f", "hello", 100, 3.14);    //"hello", 100, 3.14写入文件指针pf所指向的文本文件text中    system("pause");    return 0;}
//fscanf()函数#include<stdio.h>#include<stdlib.h>int main(){    FILE *pf = fopen("text.txt", "r");    //假设已有文件text,内容为"hello", 100, 3.14    if (pf == NULL)    {        perror("error:");        exit(EXIT_FAILURE);    }    char buf[20] = { 0 };/*给写入的数据分配地址*/    int d = 0;    float f = 0.0f;    fscanf(pf, "%s%d%f", buf, &d, &f);//将写入的数据分别存放在buf,d,f所指向的地址中    printf("%s %d %f", buf, d, f);//显示 hello 100 3.14    system("pause");    return 0;}

6.sprintf()函数和sscanf()函数

sprintf()函数的作用为格式化写入数据到某个字符串中,是变参函数。函数原型为:int sprintf( char *buffer, const char *format, [ argument] … );buffer为char型指针,指向将要写入的字符串的缓冲区;format为格式化字符串;[ argument] … 为可选参数,可以是任何类型的数据。若写入成功,则返回写入buffer的字符数,否则返回-1。
sscanf()函数的作用为从一个字符串中读进与指定格式相符的数据。其函数原型为:int sscanf( const char *buffer, const char *format [, argument ] … );该函数的buffer与sprintf()函数的buffer不同,在该函数中,buffer指向的内存区域已经存储了数据,sscanf会从buffer里读进数据,依照format的格式将数据写入argumen中。

//sprintf()函数#include<stdio.h>#include<stdlib.h>int main(){    FILE *pf = fopen("text.txt", "w");    if (pf == NULL)    {        perror("error:");        exit(EXIT_FAILURE);    }    char s[] = "hello";/*给写入的数据分配地址*/    int a = 100;    float b = 3.14;    sprintf(pf, "%s %d %f", s,&a,&b);    printf("%s %d %f ", s,a,b);    system("pause");    return 0;}
//sscanf()函数#include<stdio.h>#include<stdlib.h>typedef struct Stu    //此处用结构体来存储数据{    char name[20];    int age;    float a;}Stu;int main(){    Stu s1 = { "hello", 100, 3.14 };    Stu s2 = { 0 };    char buf[50];    sprintf(buf, "%s %d %f", s1.name, s1.age, s1.a);//将数据格式化写入字符串中,buf指向写入的字符串的缓冲区.    sscanf(buf, "%s %d %f", s2.name, &(s2.age), &(s2.a));//将buf中的数据格式化写入s2.name, &(s2.age), &(s2.a)指向的内存中    printf("%s %d %f ", s2.name, s2.age, s2.a);//显示 hello, 100, 3.14    system("pause");    return 0;}
1 0