一、打开和关闭流
FILE * fopen(const char * pathname, const char * mode);
FILE * freopen(const char * pathname, const char * mode, FILE * fp);
FILE * fdopen(int fd, const char * mode);
int fclose(FILE *fp);
int fcloseall();
例子1:
#include <stdio.h> int main(int argc, char *argv[]) { FILE *fp; int iflag; if (argc <= 1) { printf("usage: %s filename\n", argv[0]); return 1; } fp = fopen(argv[1], "r"); if (fp == NULL) { printf("Open file %s failed!", argv[1]); return 2; } printf("Open file %s succed!\n", argv[1]); iflag = fclose(fp); if (iflag == 0) { printf("Close file %s succeed!\n", argv[1]); return 0; } else { printf("Close file %s failed!\n", argv[1]); return 3; } }
|
例子2:
include <stdio.h> int main(int argc, char *argv[]) { FILE *fp1, *fp2; if ( (fp1 = fopen("file1.txt","w")) == NULL) { printf("Open file1.txt failed!\n"); return 1; } printf("Open file1.txt succeed!\n"); if ( (fopen("file2.txt","w")) == NULL) { printf("Open file2.txt failed!\n"); return 2; } printf("Open file2.txt succeed!\n"); if (fcloseall() == EOF) { printf("close file1.txt file2.txt failed!\n"); return 3; } else printf("streams closed succeed\n"); return 0; }
|
二、读和写流
1、字符I/O
intfgetc(FILE *fp);
int getc(FILE *fp);
int getchar();
int fputc(int c, FILE *fp);
int puts(int c, FILE *fp);
int putchar(int c);
2、行I/O
char *fgets(char *buf, int count, FILE *fp);
char *gets(char *buf);
int fputs(const char *str, FILE *fp);
int puts(const char *str);
3、块I/O
size_t fread(void *ptr, size_t size, size_t count, FILE *fp);
size_t fwrite(const void *ptr, size_t size, size_t count, FILE *fp);
#include <stdio.h> #define NAMESIZE 30 struct { char name[NAMESIZE]; long number; short department; short scores[10]; } student; short *pscores; int main(int argc, char *argv[]) { FILE *fpstudents; FILE *fpscore; if (argc <= 2) { printf("usage: %s sourcefile destfile\n", argv[0]); return 1; } if ( (fpstudents = fopen(argv[1],"r")) == NULL) { printf("Open sourcefile %s failed", argv[1]); return 2; } if ( (fpscore = fopen(argv[2], "w")) == NULL) { printf("Create destfile %s failed!\n", argv[2]); return 3; } while (fread(&student, sizeof(student), 1, fpstudents) == 1) { pscores = student.scores; if (fwrite(&pscores, sizeof(short), 3, fpscore) != 3) printf("Error in writing file.\n"); return 4; } return 0; }
|
三、流文件定位
long int ftell(FILE *fp);
int fseek(FILE *fp, long int offset, int whence);
void rewind(FILE *fp);
#include <stdio.h> struct record { int uid; char login[10]; }; char *logins[] = {"user1", "user2", "user3", "user4", "user5"}; void putree(FILE *fp, int i, struct record *ps) { fseek(fp, (long)i * sizeof(struct record), 0); fwrite((char *)ps, sizeof(struct record), 1, fp); } int main(int argc, char *argv[]) { int i; FILE *fp; struct record rec; if (argc <= 1) { printf("usage: %s datafile \n", argv[0]); return 1; } if ( (fp = fopen(argv[1], "w")) == NULL) { printf(" Create file %s failed!", argv[1]); return 2; } for (i=4; i>=0; i--) { rec.uid = i; strcpy(rec.login, logins[i]); putree(fp, i, &rec); } fclose(fp); return 0; }
|
int fgetpos(FILE *fp, fpos_t *pos);
int fsetpos(FILE *fp, const fpos_t *pos);
#include <stdio.h> #include <stdlib.h> char buf[132]; int main(int argc, char *argv[]) { FILE *fp; fpos_t pos; if (argc != 2) { printf("usage: %s mode\n", argv[0]); return 1; } if (argv[1][0] != 'a') { if ( (fp = fopen("testfile", "w+")) == NULL) { printf("fopen failed!\n"); return 2; } } else { if ( (fp = fopen("testfile", "a+")) == NULL) { printf("fopen failed!\n"); return 3; } } fputs("1234567890",fp); fputs("abcdefghij",fp); fseek(fp, 0, SEEK_END); fgetpos(fp, &pos); printf("current file end position is %ld\n", pos); fseek(fp, 30, SEEK_END); fgetpos(fp, &pos); printf("call fseek(fp, 30, SEEK_END)\n"); printf("current file end position is %ld\n", pos); fputs("abcdefg", fp); printf("write %c %s %c \n", '\"', "abcdefg", '\"'); fgetpos(fp, &pos); printf("current position of file is %ld\n", pos); fclose(fp); }
|