File read and write operations:
- Read a character from a file (fgetc function)
Function Prototypes:
int fgetc (FILE *FP);
2. Write a character to the file (FPUTC function)
Function Prototypes:
int FPUTC (char ch,file *FP); // writes the character ch to the file that the FP points to;
3. Reading a string from a file (Fgets function)
Function Prototypes:
Char *fgets (char *str,int n,file *FP); // read n-1 characters from the FP file into the str character array (or read to the end of the file) and add '/s ' at the end of the STR array
4. Write a string to the file (fputs function)
Function Prototypes:
int fputs (char *str,file *FP); // writes a string in str to a file in the FP reference
5.FSCANF functions and fprintf functions
Function Prototypes:
int fscanf (FILE *fp,char *format,arg); // format is a formatted string, arg is an input list for example: fscanf (FP, "%d", &i); int fprintf (FILE *fp,char *format,...); // Example: fprintf (FP, "%s", str);
In fact, fscanf and fprintf function is similar to scanf,printf function, just one from the keyboard to obtain data from a file, from a write data to the screen one write file.
6.fread functions and Fwrite functions
Function Prototypes:
int fread (void *buffer,unsigned sife,unsigned count,file *FP); // the data in the FP file is read one at a time, read count times, and put to Bufferbuffer is a void pointer that indicates the store's first address where the read-in data resides. int fwrite (void *buffer,unsigned sife,unsigned count,file *FP); //
Finally, a file read and write application: in C language (config OpenCV) Save BMP format Picture
#include <stdio.h>
#include "bmphead.h"//bmp file structure
voidSave_bitmap (Iplimage *image) { intHeight=image->height; intWidth=image->width; intImagesize= (((image->width+3)/4)*4*image->height) *3;//the size of the picture after four-byte alignment. Fileheader Fheader={0};//defining the structure headerFileInfo finfo={0};//Defining information HeadersFp=fopen ("./out_bmp.bmp","WB");//Open in binary write mode if(!FP) {printf ("cannot creat a new file!\n"); Exit (0); } //Create Bitmap File Headerfheader.bftype[0]='B'; fheader.bftype[1]='M'; Fheader.bfreserved1=0; Fheader.bfreserved2=0; Fheader.bfsize=sizeof(Fileheader) +sizeof(FileInfo) +ImageSize; Fheader.bfoffbits=sizeof(Fileheader) +sizeof(FileInfo); //Create Bitmap Info HeaderFinfo.bisize=sizeof(FileInfo); Finfo.biheight=-height; Finfo.biwidth=width; Finfo.biplanes=1; Finfo.bibitcount= -;//3-Channel color mapFinfo.bisizeimage=ImageSize; Finfo.bicompression=0; Finfo.bixpixpermeter=0; Finfo.biypixpermeter=0; Finfo.biclrused=0; Finfo.biclrimporant=0; //write infor and data into fileFwrite (&fheader,sizeof(Fileheader),1, FP); Fwrite (&finfo,sizeof(FileInfo),1, FP); Fwrite (Image->imagedata, (size_t) ImageSize,1, FP); Fclose (FP);}
Where the Bmphead.h file is as follows:
/*Bitmap Header Structure*/#pragmaPack (2)//aligned by two bytestypedefstructtagbitmapfileheader{unsignedCharbftype[2];//file FormatUnsignedLongBfsize;//File SizeUnsigned ShortbfReserved1;//reservedUnsigned ShortBfReserved2; unsignedLongBfoffbits;//the offset of the DIB data in the file}fileheader;/*Bitmap data information structure*/typedefstructtagbitmapinfoheader{unsignedLongBisize;//the size of the structure LongBiwidth;//File Width LongBiheight;//File HeightUnsigned Shortbiplanes;//Number of planesUnsigned ShortbiBitCount;//number of color bitsUnsignedLongBicompression;//Compression TypeUnsignedLongbiSizeImage;//DIB data area size LongBixpixpermeter; LongBiypixpermeter; unsignedLongbiclrused;//how many color index tablesUnsignedLongBiclrimporant;//How many important colors?}fileinfo;/*Palette Structure*/typedefstructtagrgbquad{unsignedCharRgbblue;//Blue Component BrightnessUnsignedCharRgbgreen;//Green Component BrightnessUnsignedCharrgbred;//Red Component BrightnessUnsignedCharrgbreserved;} Rgbquad;
Exposure to this knowledge and OPENCV is also due to the design of the C language course at the end of the freshman semester.
The code is placed below, there are many areas that need improvement, also hope to point out:https://github.com/oulton/college-final-project
C Language File operation and application (b)