Linux System Programming note3 —— Buffered I/O

来源:互联网 发布:三笠宫崇仁亲王知乎 编辑:程序博客网 时间:2024/05/23 00:04
1. Opening Files
#include <stdio.h>

FILE *fopen(const char *path, const char *mode);
Modes:
r, r+, w, w+, a, a+, b

2. OPening a Stream via File Descriptor
#include <stdio.h>

FILE *fdopen(int fd, const char *mode);

3. Closing Streams
#include <stdio.h>

int fdclose(FILE *stream);

4. Closing All Streams
#define _GNU_SOURCE
#include <stdio.h>

int closeall(void);

5. Reading a Character at a Time
#include <stdio.h>

int fgetc(FILE *stream);

6. Putting the character back
#include <stdio.h>

int ungetc(int c, FILE *stream);

7. Reading a Entire Line
#include <stdio.h>

char *fgets(char *str, int size, FILE *stream);

8. Reading Binary Data
#include <stdio.h>

size_t fread(void *buf, size_t size, size_t nr, FILE *stream);

9. Writing a Single Character
#include <stdio.h>

int fputc (int c, FILE *stream);

10. Writing a String of Characters
#include <stdio.h>

int fputs(const char *str, FILE *stream);

11. Writing Binary Data
#include <stdio.h>size_t fwrite(void *buf, size_t size, size_t nr, FILE *stream);#include <stdio.h>int main(void){     FILE *in, *out;     struct pirate{          char          name[100];          unsigned long booty;          unsigned int  beard_len;     }p, blackbeard = { "Edward Teach", 950, 48 };     out = fopen("data", "w");     if (!out) {          perror("fopen");          return 1;     }     if (!fwrite(&blackbeard, sizeof(struct pirate), 1, out)){          perror("fwrite");          return 1;     }     if (fclose(out)){          perror("fclose");          return 1;     }     in = fopen("data", "r");     if (!in){          perror("fopen");          return 1;     }     if (!fread(&p, sizeof(struct pirate), 1, in)){          perror("fread");          return 1;     }     if (fclose(in)){          perror("fclose");          return 1;     }     printf("name = \"%s\" booty = %lu beard_len = %u\n", p.name, p.booty, p.beard_len);     return 0;}


12. Seeking a Stream

#include <stdio.h>

int fseek(FILE *stream, long offset, int whence);
whence: SEEK_SET, SEEK_CUR, SEEK_END

#include <stdio.h>
int fsetpos (FILE *stream, fpos_t *pos);

#include <stdio.h>
void rewind(FILE *stream); //resets the position back to the start of the stream

13. Obtaining the Current Stream Position
#include <stdio.h>

long ftell(FILE *stream);

#include <stdio.h>

int fgetpos(FILE *stream, fpos_t *pos);

14. Flushing a Stream
#include <stdio.h>

int fflush(FILE *stream);

15. Errors and End-of-File
#include <stdio.h>

int ferror(FILE *stream);

#include <stdio.h>

int feof(FILE *stream);

#include <stdio.h>

void clearerr(FILE *stream);

16. Obtaining the Associated File Descriptor
#include <stdio.h>

int fileno(FILE * stream);
17. Controlling the Buffering
Unbuffered, Line-buffered, Block-buffered
注:By default, all streams associated with files are block-buffered.

#include <stdio.h>

int setvbuf(FILE *stream, char *buf, int mode, size_t size);
mode:
_IONBF
_IOLBF
_IOFBF

18. Manual File Locking
#include <stdio.h>

void flockfile(FILE *stream);

#include <stdio.h>
void funlockfile(FILE *stream);

#include <stdio.h>

int ftrylockfile(FILE *stream);

19.
#define _GNU_SOURCE
#include <stdio.h>

int fgetc_unlocked(FILE *stream);
char *fgets_unlocked(char *str, int size, FILE *stream);
size_t fread_unlocked(void *buf, size_t size, size_t nr, FILE *stream);
int fputc_unlocked(int c, FILE *stream);
int fputc_unlocked(const char *str, FILE *stream);
size_t fwrite_unlocked(void *buf, size_t size, size_t nr, FILE*stream);
int fflush_unlocked(FILE *stream);
int feof_unlocked(FILE *stream);
int ferror_unlocked(FILE *stream);
int fileno_unlocked(FILE *stream);
void clearerr_unlocked(FILE *stream);

0 0
原创粉丝点击