Fgetc, fgets, fputc, fputs, fread, fwrite, fseek, fprintf

Source: Internet
Author: User

Format: int fgetc (File * stream );

Indicates reading a character from the object pointed to by the file pointer stream.

The return value of this function is a byte read.. If EOF is returned at the end of the file.

The prototype is char * fgets (char * s, int N, file * stream );

Read from streamN-1The parameter S is used to receive strings. If the string is successfully received, the pointer of S is returned. Otherwise, null is returned.

Fputc ()

Usage: int fputc (int ch, file * stream );

Returned value: If the write is successfulReturns the written characters., EOF.

Function Name: fputs

Function: Send a string to a stream.

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

Return Value:

On success, a non-negative value is returned.

Fputs. The value returned after successful writing is non-negative. If it is not negative, it is positive or 0.

A positive number or 0 is determined by your compiler, and the meaning of the positive value is determined by the compiler.

On error, the function returns EOF.

The return value for write failure is EOF.

Fread

Function: read data from a stream

Function prototype: size_t fread (void * buffer, size_t size, size_t count, file * stream );

Parameters:

1. The address (pointer) (buffer) used to receive data)

2. Size of a single element: the unit is byte rather than bit. For example, the number of read int-type in data is 4 bytes.

3. Number of elements (count)

4. Provide the data file pointer (Stream)

Returned value: number of successfully read Elements

Eg: fread (BUF,Strlen (MSG ),1, stream); note that + 1 is not required

Printf ("% s \ n", Buf );

Fwrite

Size_t fwrite (const void * buffer, size_t size, size_t count, file * stream );

Note: This function operates files in binary format, not limited to text files.

Return Value: returns the number of actually written data blocks.

(1) buffer: A pointer. For fwrite, It is the address for outputting data.

(2)Size: Number of Single-word segments to be written;

(3)Count: number of data items to write size bytes;2 does not indicate writing two buffers to the file stream.

(4) stream: pointer to the target file.

(5) return the number of actually written data items count

Eg. fwrite (string,Strlen (string ),1, stream); (char string [] = "this is a test ";)Do not add 1

Fwrite (& S, sizeof (s), 1, stream);/* write struct s to file */

Int fseek (File * stream, long offset, int fromwhere );

Seek_set: Start of the file

Seek_cur: current location

Seek_end: End of the file

Seek_set, seek_cur, and seek_end are respectively 0, 1 and 2.

In short:

Fseek (FP, 100l, 0); move the internal pointer of the file to 100 bytes away from the beginning of the file;

Fseek (FP, 100l, 1); move the internal pointer of the file to 100 bytes away from the current position of the file;

Fseek (FP,-100l, 2); returns the internal pointer of the file to the first byte from the end of the file.

The function sets the position of the file pointer stream. If the execution is successful, stream points to the position of fromwhere (starting position of the Offset: File Header 0, current position 1, end 2) as the benchmark, and offset (pointer offset) bytes. If the execution fails (for example, the offset value exceeds the file size), the position pointed to by the stream is not changed.

0 is returned. Otherwise, other values are returned.


Function Name: fscanf

Function: format input from a stream

Usage: int fscanf (File * stream, char * format, [argument...]);

Int fscanf (File pointer, Format String, input list );

Return Value: integer. The value is equal to the number of [argument ...]

Eg: int I; fscanf (stdin, % d, & I );

Fprintf

Int fprintf (File * stream, const char * format ,...);

The fprintf () function sends information (parameters) to the file specified by stream based on the specified format (format.

Fprintf () can only work the same way as printf (). The return value of fprintf () is the number of output characters. If an error occurs, a negative value is returned.

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.