C and pointer: IO details, pointer io details

Source: Internet
Author: User

C and pointer: IO details, pointer io details
1. Stream

Io operations are simple operations to import or remove bytes from a program. This byte stream is called a stream.

2. Two types of stream: Text Stream and binary stream 1) Text Stream: Text Stream refers to the data flowing in the stream as characters
2) binary stream: a binary stream refers to a stream of binary numbers. If a stream contains only characters, it is represented by a byte binary ASCII code. If it is a number, it is represented by the corresponding binary number. The \ n symbol is not transformed during inbound and outbound traffic.
For example, the number 5678 is represented by its ASCII code in a text stream:
5678 Storage Format: ASCII code: 00110101 00110110 00110111 00111000 (four bytes)
5678 Storage Format: Binary: 00010110 00101110 (two bytes)
4 bytes in total. In the binary stream, 00000111 11010001 is expressed as 07D1 in hexadecimal format. The binary stream only occupies two bytes, which saves space than the text stream and does not need to convert \ n, which can greatly speed up the stream and improve the efficiency. Therefore, binary streams can be used for digital streams that contain a large amount of digital information. Text streams can be used for streams that contain a large amount of character information.
1) features of Text Stream: Maximum length of text lines, with a standard of at least 24 characters. End mode. In the DOS system, it ends with a carriage return and a linefeed. unix only applies to one linefeed.
The computer is physically binary, so the difference between a text file and a binary file is not physical, but logical. The two are only different at the encoding level.
In C, text or binary reading and writing are the interaction between the buffer and the binary stream in the file, but there is a line break conversion during text reading and writing. therefore, when there is no linefeed ''\ n' (0AH) in the write buffer, the text Write result is the same as the binary Write result. Similarly, when ''\ r \ n' (0DH0AH) does not exist in the file, the text read result is the same as the binary read result.
3. Three basic forms of io stream processing 1. Single Character
Int fgetc (FILE * stream)
Int fputc (int character, FILE * stream );
Int getc (FILE * stream)
Int putc (int character, FILE * stream );
Int getchar (void)
Int putchar (int character );
2. Text lines
I/o of unformatted rows
Char * fgets (char * buffer, int buffer_size, FILE * stream );
Char * gets (char * buffer );
Int fputs (char const * buffer, FILE * stream );
Int puts (char const * buffer );
Format I/o
Int fscanf (FILE * stream, char const * format ,...);
Int scanf (char const * format ,...);

Int sscanf (char const * string, char const * format ,...);

4. Some demos

# Include
 
  
# Include
  
   
/** If the opened FILE is not found **/int main () {FILE * input; input = fopen ("data20", "r "); if (input = NULL) {perror ("data3"); exit (EXIT_FAILURE);} return 0 ;}/ ** 1111deMacBook-Pro: Music a1111 $ gcc-g fopen. c-o fopen1111deMacBook-Pro: Music a1111 $. /fopendata3: No such file or directory **/
  
 
# Include
 
  
# Include
  
   
// Convert the number int read_int () {int value; int ch; while (ch = getchar () read from the standard input ())! = EOF & isdigit (ch) {value * = 10; value + = ch-'0';} return value;} int main () {int value = read_int (); printf ("value is % d \ n", value); return 0;}/** 1111deMacBook-Pro: Music a1111 $ gcc-g getc. c-o getc1111deMacBook-Pro: Music a1111 $. /getc1233value is 12331111deMacBook-Pro: Music a1111 $ **/~
  
 
# Include
 
  
# Define MAX_LENGTH 1024 // copy the FILE void copylines (FILE * input, FILE * output) {char buffer [MAX_LENGTH]; while (fgets (buffer, MAX_LENGTH, input )! = NULL) fputs (buffer, output);} int main () {FILE * input = fopen ("data2", "r"); if (input! = NULL) {FILE * output = fopen ("data3", "w"); if (output! = NULL) {copylines (input, output); fclose (output);} else puts ("data3 is NULL"); fclose (input );} else puts ("data2 is NULL"); return 0 ;}
 
# Include
 
  
Int main () {char str [512] = {0}; sscanf ("123456", "% 4 s", str ); printf ("str is % s \ n", str); // sscanf ("123aaadf4aD45dbcdBDD", "% [^ A-Z]", str ); printf ("str is % s \ n", str); return 0;}/** 1111deMacBook-Pro: Music a1111 $ vim sscanf. c1111deMacBook-Pro: Music a1111 $ gcc-g sscanf. c-o sscanf1111deMacBook-Pro: Music a1111 $. /sscanfstr is 1234str is 123aaadf4a **/
 
# Include
 
  
# Include
  
   
// Format the data input to the text FILE * stream; int main (void) {int I = 15; double fp = 15.5; char s [] = "hello word "; char c = '\ n'; stream = fopen ("fprintf.txt", "w"); fprintf (stream, "% s % c", s, c ); fprintf (stream, "% d \ n", I); fprintf (stream, "% f \ n", fp); fclose (stream); return 0 ;} /** hello word1515.500000 **/
  
 
# Include
 
  
// Format the input data to the text int main () {FILE * fp; int I = 117; char * s = "chenyu"; fp = fopen ("text. dat "," w "); fputs (" total ", fp); fputs (": ", fp); fprintf (fp," % d \ n ", I ); fprintf (fp, "% s", s); fclose (fp); return 0;}/** total: 117chenyu **/
 
# Include
 
  
// Ftell is used to obtain the offset of the current position of the FILE pointer to the first byte of the FILE. int main (void) {FILE * stream; stream = fopen ("MYFILE. TXT "," w + "); fprintf (stream," This is a test "); printf (" The file pointer is at byte % ld \ n ", ftell (stream); fclose (stream); return 0 ;}/ ** 1111deMacBook-Pro: Music a1111 $ vim ftell. c1111deMacBook-Pro: Music a1111 $ gcc-g ftell. c-o ftell1111deMacBook-Pro: Music a1111 $. /ftellThe file pointer is at byte 14 **/
 
Int fprintf (FILE * stream, char const * format ,...);
Int printf (char const * format ,...);
Int sprintf (char * buffer, char const * format ,...);
3. binary data
Size_t fread (void * buffer, size_t size, size_t count, FILE * stream );
Size_t fwrite (void * buffer, size_t, size, size_t count, FILE * sread );


Related Article

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.