读写文件1——以字节为单位,读写文件(笔记实例)
start
#include <stdio.h>
int fseek(FILE *stream, long offset, int whence);
//第二个参数和第三个参数决定将要移动到的位置, 操作失败返回-1
//第二个参数是移动的字节数(正数向文件尾移动,负数向,文件头移动。
//第三个参数,是开始移动的位置,SEEK_SET(文件头),SEEK_CUR(指针当前位置),SEEK_END(文件尾)
long ftell(FILE *stream); 获取当天的读写位置 操作失败返回-1
void rewind(FILE *stream); 读写位置,设置在文件头,文件开头位置是
[cpp]
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
void openfile(FILE **file, const char*filename);
void closefile(FILE *file); void openfilewithpermisson(); void openfiledir(); void wirtefile(FILE *);
void readfile(FILE *);
void readfile_endchar(FILE *);
int main(){
FILE *file = NULL;
const char fname[] = "lang.txt";
openfile(&file,fname); printf("-->> file = %p \n",file);
wirtefile(file);
readfile(file);
readfile_endchar(file);
closefile(file);
//openfilewithpermisson();
//openfiledir();
return 0;
}
void readfile_endchar(FILE *p_file){
int cur_position = -1;
int err = fseek(p_file,-1,SEEK_END);
if(err==EOF){
perror("fseek failed");
return;
}
int ch = fgetc(p_file);
putchar(ch);
printf("\n ch = %d,\n", ch);
err = fseek(p_file,-1,SEEK_END);
if(err==EOF){
perror("fseek failed");
return;
}
cur_position = ftell(p_file); //读取当前读写位置。
if(cur_position==EOF){
perror("ftell failed");
return;
}
printf("cur_position = %d,\n",cur_position);
rewind(p_file); //读写位置,设置在文件头,文件开头位置是
cur_position = ftell(p_file);
if(cur_position==EOF){
perror("ftell failed");
return;
} printf("cur_position = %d,\n",cur_position);
}
void wirtefile(FILE *file){
printf("-->> write start \n");
int num = 5;
while(num!=0){
fputc(num,file);
num--;
}
rewind(file);
printf("-->> write end, num = %d \n", num );
}
void readfile(FILE * file){
printf("-->> read start \n");
int c = -1;
while((c=fgetc(file))!=EOF){
putchar(c);
printf(" \n");
// printf("read c = %d \n",c);
}
printf("-->> read end \n");
}
void openfiledir(){
FILE * p_filedir;
char filedir[] = "/home/langu/linuxk/";
if(p_filedir==NULL){
perror("open filedir");
return;
}
// perror("open filedir");
printf("open filedir success \n");
}
void openfilewithpermisson(){
FILE *filedir;
char filename[] = "/etc/gshadow";
filedir = fopen(filename, "r");
if(filedir==NULL){
perror("etc/gshadow");
return ;
}else{
printf("etc/gshadow success \n ");
}
}
void openfile(FILE **p_file, const char *path){ //要传址所以用二重指针
FILE *file;
file=fopen(path, "w+");
if(file==NULL){
fputs(strerror(errno),stderr);//
printf(" \n");
perror("open file lang.txt");
printf("-->> error: %d \n", errno);
return ;
}
*p_file = file;
}
void closefile(FILE *file){
if(fclose(file)==EOF){
perror("close file");
printf("-->> close file failed \n");
}
printf("-->> close file success \n");
}
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
void openfile(FILE **file, const char*filename);
void closefile(FILE *file); void openfilewithpermisson(); void openfiledir(); void wirtefile(FILE *);
void readfile(FILE *);
void readfile_endchar(FILE *);
int main(){
FILE *file = NULL;
const char fname[] = "lang.txt";
openfile(&file,fname); printf(&quo
补充:软件开发 , C++ ,