Start
# Include <stdio. h>
Int fseek (FILE * stream, long offset, int whence );
// The second and third parameters determine the location to be moved. If the operation fails,-1 is returned.
// The second parameter is the number of bytes to be moved (moving positive toward the end of the file, negative toward, and moving the file header.
// The third parameter is the start position, SEEK_SET (File Header), SEEK_CUR (current position of the pointer), and SEEK_END (File tail)
Long ftell (FILE * stream); returns-1 if the read/write location operation fails to be obtained on the current day.
Void rewind (FILE * stream); read/write location, which is set in the FILE header and starts
[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); // read the current read/write location.
If (cur_position = EOF ){
Perror ("ftell failed ");
Return;
}
Printf ("cur_position = % d, \ n", cur_position );
Rewind (p_file); // read/write location, which is set in the file header and starts
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) {// use a double pointer to transfer an address.
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); // read the current read/write location.
If (cur_position = EOF ){
Perror ("ftell failed ");
Return;
}
Printf ("cur_position = % d, \ n", cur_position );
Rewind (p_file); // read/write location, which is set in the file header and starts
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) {// use a double pointer to transfer an address.
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 ");
}
Running result:
Because one byte reads one byte, the two files store four bytes of int,
Therefore, when output in fputc, the integer data is printed in four bytes.
End