Fget (STR, N, FD) and stdin stdout

Source: Internet
Author: User
Tags processing text

1. Function Name: fgets

Function: Read a string from the stream.

Usage: char * fgets (char * string, int N, file * stream );

Annotation of the parameter: * The first address of the string input data; n-1: the length of the data block read at a time. The default value is 1 K, that is, 1024; stream file pointer

Example:

# Include & lt; string. H & gt;

# Include & lt; stdio. H & gt;

Int main (void)

{

File * stream;

Char string [] = "this is a test ";

Char MSG [20];

/* Open a file for update */

Stream = fopen ("dummy. fil", "W + ");

/* Write a string into the file */

Fwrite (string, strlen (string), 1, stream );

/* Seek to the start of the file */

Fseek (stream, 0, seek_set );

/* Read a string from the file */

Fgets (MSG, strlen (string) + 1, stream );

/* Display the string */

Printf ("% s", MSG );

Fclose (Stream );

Return 0;

}

The fgets function fgets is used to read strings from files. The fgets function is called as follows: fgets (STR, N, FP); here, FP is the file pointer; STR is
The starting address of the string. N is an int type variable. The function is to read n-1 characters from the file indicated by FP into the space where STR is the starting address. n-1 characters are not fully read
When a line break or an EOF is read, The read operation is ended. The string to be read contains the line break. Therefore, to be exact, call the fgets Function
You can only read n-1 characters at most. After reading, the system automatically adds '/0' at the end and returns the result using STR as the function value.

Stdin

STD = stand in = Input
Stdin is a standard input device, generally a keyboard.

Stdin is a redefinition,
Defined as a standard input device (that is, a drive)
Stdout
Standard output device (Display)
The W + parameter is the read/write mode for opening a file/

Example: A simple applet that can input characters from the keyboard and then send them to the console. Using Perl, you can write as follows:
While ($ line = <stdin>)
{
Print $ line;
}
To put it simply, <stdin> is a dedicated file handle. In this example, it belongs to the standard input cache (called
Is stdin), usually connected to the keyboard. Every time we assign the value of <stdin> to the variable $ line, it is to extract the first line in the stdin cache and then put
Enter $ line. When the last row of the cache is reached, the end identifier of the (EOF) file that is false when the while statement is judged. The rest of the program can be easily explained in this way. Now we have
Put the input information into the $ line variable, and then we can print it to the screen using the print function. Or, more accurately, print to the standard input cache that is normally connected to the monitor. Standard Input and Output slowdown
Both can be redirected. For example, you can redirect to a file that saves the program results. If you are processing text, you can usually think that this is equivalent to the keyboard and display.


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.