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("-->> 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");
}
運行結果:
因為是一個字節一個字節的讀取,二文件中的是存儲的是int四個字節,
所以用fputc輸出時,把整型的數據,按照四個字節形式打印。
end