10, C language--bit operation and file

Source: Internet
Author: User
Tags bitwise bitwise operators fread

Tag: operator uppercase bit value print indicates and file is specified

Bitwise operations and files

One, bit arithmetic
The operand of a bitwise operation can only be integer or character-type data
The C language provides 6 kinds of bitwise operators:
&| ^~<<>>
Compound assignment operators:
&=|=^=<<=>>=
1. Bitwise and arithmetic (&)
Two corresponding binaries are 1 o'clock, and their bitwise operations result in 1, otherwise 0
Function: Clear Zero

2. Bitwise OR Operation (|)
Two corresponding binary as long as one is 1 o'clock, then they bitwise or the result of the operation is 1
Function: Place a specific position 1

3. Bitwise XOR or operation (^)
When two corresponding bits are the same as 1 or 0 o'clock, the bitwise XOR or operation result is 0, two corresponding bits one is 1 and the other is 0 o'clock, bitwise XOR OR operation result is 1

4, bitwise negation operation (~)
The bitwise inverse operator is a single-mesh operator, which is reversed after 0 changes to 0
Note:
The value of a bitwise inverse is the number +1 multiplied by-1

5. Bitwise left SHIFT operation (<<)
Format: Number <<n
Function: Shift the bits to the left n bit by bit
The value obtained by moving the N-bit to the left of a decimal number is the number multiplied by the product of the 2^n

6. Bitwise Right SHIFT operation (>>)
Format: Number >>n
Function: Shift bits to right by n bits
If the number is a negative and cannot be divisible by 2^n, the number is Shangga-1

Document one, document
1. Files: A collection of data recorded on external storage media (external memory)
2, the classification of documents:
Text files (ASCII code files): Each character is stored in a file
Binary files: binary storage
The access to files in the C language is in characters (bytes).

3. File Type pointer
In the exam, remember that file is a type name, which is a struct type. A file is manipulated by a file pointer.
Second, the opening and closing of the file
1. Open the file (fopen () function)
Format: fopen (file name, how to use files)
Function: Opens a file in the specified usage mode, and the function returns a pointer to the open file, the base type of the pointer is a file type, and the file name and files are used in a string
Open "How to use" File
"R"
"W"
A
"+"
Note:
1) files can only be used in lowercase letters, the file name in uppercase or lowercase are the same
2) in "File usage" if it contains the letter B, the Open is a binary file (bit)
3) when fopen "open" fails, the function returns null

2. Closing the file (fclose function)
You should close the file after you have finished using it
Format: fclose (file pointer)

Third, read and write the file operation
The file can be manipulated when it is opened. The operation of files in C language is implemented by library functions.
A character in a read/write file Fputc (), fgetc ()
Format: FPUTC (character, file pointer)
Function: Writes a character to the file referred to in the file pointer. Where characters can be character constants or character variables. If the output succeeds, the function returns the output character, and the failure returns EOF
Format: fgetc (file pointer)
Function: Reads a character from the file indicated by the document pointer. If the read succeeds, the function returns the read character, and fails (the end of the file is encountered) to return EOF

A string in the read/write file fputs (), fgets ()
Format: fputs (STR,FP)
Function: Writes (outputs) a string in str to a file referred to in the FP, str can be a string constant, a character array, or a character pointer variable. The terminator of the string at output is not output. Returns 0 if the output succeeds, and the failure returns EOF
Format: fgets (STR,N,FP)
Function: where str represents a character pointer, it can be a character array name or a character pointer variable name. N-1 characters (not n characters) are read from the FP file and assigned to STR after the end of these characters with a string Terminator ' + '
function returns the first address of STR

Read/write a block of data in a file Fread (), fwrite ()
Format: fread (BUFFER,SIZE,COUNT,FP)
Format: fwrite (BUFFER,SIZE,COUNT,FP)
which
Buffer is the address of the data
Size is the number of bytes per read and write
Count indicates how many times the function is read and written
FP is a contiguous block of data to be read and written
Note:
1) These two functions are read and written in binary mode

Format the file read/write fprintf (), fscanf ()
Format:
fprintf (file pointer, format specifier, output list)
FSCANF (file pointer, format specifier, input list)
Function: Read and write (input and output) data to a file in the format specified by the format specifier. Where the format specifier and input () output list are used in the same way as the scanf and printf functions

Add:
feof (file pointer)
The function is to test whether the current read and write location of the file is at the end of the file, or if it returns a value other than 0 (true), otherwise 0 (false)

Iv. file current read and write position function
1) Reposition the pointer
Format: Rewind (file pointer)
Function: Causes the current read and write position to point back to the beginning of the file. function has no return value

2) fseek ()
Format: fseek (file pointer, displacement amount, starting point)
Function: Moves the current read and write location from start point to the number of moved bytes specified by displacement amount

The starting points are:
Seek_set or 0 (indicates "start of File")
Seek_cur or 1 (indicates "current position")
Seek_end or 2 (indicates end of file)
Displacement amount: To add the letter L or L after the value

3) Ftell (file pointer)
Function: Returns the position of the current file, expressed as the amount of displacement relative to the header of the file. If returning -1l indicates an error

10, C language--bit operation and file

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.