C language file reading, writing and positioning functions

File function

File reading

Fgetc (read one character)

// Function prototype
int fgetc(
FILE *stream     //Pointer to file structure
);
  1. fgetc returns characters read as int, or EOF to indicate an error or end of file.
  2. This function will move the file pointer to the next character after reading.
while(EOF != (ch = fgetc(fp))){     //All characters of the file can be read
		printf("%c",ch);            //If the output is garbled, the file code can be adjusted to ANSI
	}

Fgets (read a line of string)

// Function prototype
char *fgets(
   char *str,           //Storage location of data.
   int numChars,        //The maximum number of characters to read.      
   FILE *stream         //Pointer to FILE structure
);
  1. When the function is called, it reads up to n-1 characters and stores the read string in n-1 consecutive memory units starting from the memory address pointed to by the pointer str.

  2. When the function reads the specified number of strings, or receives a newline character, or receives the end of file flag EOF, the '\0' character will be automatically added after the read character. If there is a newline character, the newline character will be retained (the newline character is before the '\0' character). If there is EOF, EOF is not retained.

  3. After successful execution, the function will automatically return the read string to str. If the execution fails, NULL is returned to indicate an error or end of file condition.

  4. Function will move the file pointer to the next character of the string after reading.

char str[100];
while(fgets(str,100,fp) != NULL){     //Read all contents of the file
		printf("%s",str);
	}

FREAD (read a string with a specified number of bytes)

//Function prototype
size_t fread(
   void *buffer,    //Storage location of data.
   size_t size,     //Item size (in bytes), that is, how many bytes are read each time.
   size_t count,    //The maximum number of items to read, that is, how many times.
   FILE *stream     //Pointer to the FILE structure.
);

fread returns the complete number of items actually read. If an error occurs before reaching buffer * size or the end of the document is encountered, the number may be less than buffer * size. If it is finished, it returns 0.

// Read the entire contents of the file.
char str[100] = {0};
while(fread(str,1,99,fp)){
    printf("%s",str);
}

Writing of documents

When you open a file with W, w+, the file will be emptied.

Fputc (write one character)

//Function prototype
int fputc(
   int c,               //The character to write.
   FILE *stream         //Pointer to the file structure.
);

The function returns the written character. fputc, the return value EOF indicates an error.

Fputs (write a string)

//Function prototype
int fputs(
   const char *str,         //Output string.
   FILE *stream             //Pointer to the FILE structure.
);

If successful, the function will return a non negative value. fputs will return EOF when an error occurs.

char*str = "abcdefghijklmnopqrstuvwxyz";
fputs(str,fp);

Fwrite (write with specified number of bytes)

//Function prototype
size_t fwrite(
   const void *buffer,      //Pointer to the data to be written.
   size_t size,
   size_t count,
   FILE *stream
);

fread returns the number of complete items actually written. If an error occurs or the end of the document is encountered before reaching buffer * size, this number may be less than buffer * size.

//Write numbers
int num = 12345;
fwrite(&num,1,sizeof(num),fp);
//Write structure
struct student{
    int num;
    char name[];
    int grade; 
};
struct student a = {1,"xjh",100};
fwrite(&a,1,sizeof(a),fp);

Location of files

rewind(fp)

Reset the file pointer to the beginning of the file

ftell(fp)

Return to current position

fseek

//Function prototype
int fseek(
   FILE *stream,     //Pointer to the FILE structure.
   long offset,      //The number of bytes starting from origin.
   int origin        //Initial position.
);

If successful, fseek returns 0. Otherwise, a non-zero value is returned.

The value of origin can only be

Original valuemeaning
SEEK_CURThe current position of the file pointer.
SEEK_ENDEnd of file.
SEEK_SETThe beginning of the file.
fseek(fp,-10,SEEK_CUR);    //Position the pointer 10 bytes ahead of the current position
fseek(fp,5,SEEK_SET);      //Position the pointer five bytes backward from the beginning of the file
fseek(fp,-10,SEEK_END);    //Position the pointer 10 bytes forward of the end of the file
// Implement the last line of output
#include<stdio.h>
#include<stdlib.h>

int main(){
   FILE*fp = fopen("C:\\Users\\1\\OneDrive\\desktop\\z.txt","r+");
   if(fp == NULL){
      printf("fail to open file.\n");
   }	

   int Length = -1;
   char Find;
   fseek(fp,Length,SEEK_END);
   while(fread(&Find,1,1,fp)){
      if(Find == '\n'){
         break;
      }
      Length--;
      fseek(fp,Length,SEEK_END);
   }
   jiLu++;
   fseek(fp,jiLu,SEEK_END);
   char str[100] = {0};
   fgets(str,99,fp);
   printf("%s",str);

   fclose(fp);

	system("pause");
	return 0;
}

Tags: C C++

Posted by vyb3 on Tue, 09 Aug 2022 13:34:51 +0530