Several C functions: fgets, sscanf, fread, fwrite, strncpy, fopen, sprintf

Source: Internet
Author: User
Tags fread
(1) fgets:
Header file Include <stdio. h>
Define functions Char * fgets (char * s, int size, FILE * stream );
Function Description Fgets () is used to read characters from the file referred to by the parameter stream to the memory space referred to by the parameter s until a line break occurs, the end of the file is read, or the size-1 character is read, finally, NULL is added as the string.
Return Value If fgets () is successful, the s pointer is returned. If NULL is returned, an error occurs.


If the size is <= 0, NULL is returned.
If size = 1, "" is returned, which is an empty string.
If the successful return value is equal to string, that is, the first address of the obtained string.
If an error occurs or the end of the FILE is read, NULL is returned.

The returned data starts from the current position of the file pointer until the Enter key is encountered. In this case, the file pointer stream is located in the byte next to the Enter key.

(2) sscanf (Format String input)

Header file # Include <stdio. h>
Define functions Int sscanf (const char * str, const char * format ,........);
Function Description Sscanf () converts and formats the str string based on the format string. For more information about format conversion, see scanf (). The converted result is stored in the corresponding parameter.
Return Value If the call succeeds, the number of parameters is returned. If the call fails,-1 is returned. The error cause is stored in errno.

(3) fread (reads data from the file Stream)

Header file # Include <stdio. h>
Define functions Size_t fread (void * ptr, size_t size, size_t nmemb, FILE * stream );
Function Description Fread () is used to read data from a file stream. The stream parameter is an opened file pointer. the ptr parameter points to the data space to be read. The number of characters read is determined by the size * nmemb parameter. Fread () returns the number of nmemb actually read. If this value is smaller than the nmemb parameter, it indicates that the file may be read at the end or an error occurs. In this case, feof () must be used () or ferror () to determine what happens.
Return Value Returns the number of nmemb instances actually read.

(4) fwrite (write data to a file Stream)

Header file # Include <stdio. h>
Define functions Size_t fwrite (const void * ptr, size_t size, size_t nmemb, FILE * stream );
Function Description Fwrite () is used to write data into the file stream. The stream parameter is an opened file pointer. the ptr parameter points to the data address to be written. The total number of characters written is determined by the parameter size * nmemb. Fwrite () returns the number of nmemb actually written.
Return Value Returns the number of nmemb actually written.

C language for reading and writing the entire data. It can be used to read and write a group of data, such as an array element and a value of a structure variable. The call Method for reading data block functions is fread (buffer, size, count, fp). The call Method for writing data block functions is fwrite (buffer, size, count, fp); buffer is a pointer. In the fread function, it indicates the first address to store the input data. In the fwrite function, it indicates the first address of the output data. Size indicates the number of bytes of the data block. Count indicates the number of data blocks to read and write. Fp indicates the file pointer. The file pointer fp refers to the read byte.

Strncpy and strcpy do not automatically end with '/0 '.

Implementation Code of strcpy
Char * strcpy (char * strDest, const char * strSrc)
{
If (strDest = NULL) | (strSrc = NULL) // [1]
Throw "Invalid argument (s)"; // [2]
Char * strDestCopy = strDest; // [3]
While (* strDest ++ = * strSrc ++ )! = '/0'); // [4]
Return strDestCopy;
}
Add '/0'

The semantics of memcpy is that it faithfully copies N Bytes regardless of whether the content contains '/0.
The syntax of strncpy is to copy N Bytes. If '/0' is encountered in the middle, it will end early.

Fopen is automatically created if the file does not exist.

Strlen does not include the '/0' of the string'

Print the integer 123 into a string and save it in s.
Sprintf (s, "% d", 123); // generate "123"

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.