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
- Linux System Programming note3 —— Buffered I/O
- [Unix/Linux Programming] Buffered I/O vs Unbuffered I/O
- Linux System Programming note 2 ——File I/O
- Linux System Programming note 4 —— Advanced File I/O
- Buffered I/O and non-buffered I/O
- Buffered I/O and non-buffered I/O
- Buffered I/O and non-buffered I/O
- unbuffered I/O. buffered I/O
- 标准I/O(buffered I/O)浅析
- 标准I/O(buffered I/O)浅析
- unbuffered I/O. buffered I/O<转载>
- unbuffered I/O. buffered I/O<转载>
- 标准I/O(buffered I/O)浅析
- System Level I/O && Network Programming
- 【简记】Operating System—— I/O
- tcflush与unbuffered I/O,buffered I/O
- tcflush与unbuffered I/O,buffered I/O
- [Java Performance] 缓冲I/O(Buffered I/O)
- JS 实现远程文件下载
- jar命令生成可执行jar文件
- 关于onSaveInstanceState与onRestoreInstanceState
- Linux System Programming note 2 ——File I/O
- 寻ta驿站成长记二
- Linux System Programming note3 —— Buffered I/O
- 审对23案81名刑事被告人
- 精简菜单和完整菜单之间进行切换
- 1207: [HNOI2004]打鼹鼠
- 多浏览器测试对比工具
- 互联网思维:3个片段,9大招式,19条法则
- Struts2的工作原理
- Linux System Programming note 4 —— Advanced File I/O
- 关于数据结构的学习