3-b-io.c

来自「Linux下的C代码 实现了对Linux的文件系统的操作」· C语言 代码 · 共 49 行

C
49
字号
#include <stdio.h>#include <stdlib.h>#include <string.h>void err_quit(char* msg);int main(void){	 FILE* fp;	 char *buffer;	 char c;	 int i;	 	 if ((buffer = (char*)malloc(1024)) == NULL)		  err_quit("malloc error");	 	 if ((fp = fopen("tempfile2", "w+")) == NULL)		  err_quit("fopen error");	 	 for (i=1; i<=100; i++)		  sprintf(buffer+strlen(buffer), "%d", i);	 	 if (fprintf(fp, "%s", buffer) < 0)		  err_quit("fprintf error");	 if (fseek(fp, 50, SEEK_SET) < 0)		  err_quit("fseek error");	 c = fgetc(fp);	 printf("The 50th byte is: %c\n", c);	 if (fseek(fp, 100, SEEK_SET) < 0)		  err_quit("fseek error");	 c = fgetc(fp);	 printf("The 100th byte is: %c\n", c);	 fclose(fp);	 if(remove("tempfile2") < 0)		  err_quit("remove error");	 	 return 0;}void err_quit(char* msg){	 fprintf(stderr, msg);	 exit(1);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?