File Operations fopen, fclose, Fread, Fwrite, Fseek, Ftell

Source: Internet
Author: User
Tags fread
fopen
FILE * fopen (const char * filename, const char * mode);


fclose

int fclose (FILE * stream);


fread
size_t fread (void * ptr, size_t size, size_t count, FILE * stream);
Read block of data from stream reads an array to count elements, each one with a size of size bytes, from the stream an D stores them in the blocks of memory specified by PTR.

The position indicator of the stream is advanced by the total amount of bytes read.

The total amount of bytes read if successful is (Size*count).

fwrite

size_t fwrite (const void * ptr, size_t size, size_t count, FILE * stream);
Write block of data to stream writes a-array of count elements, each one with a size of size bytes, from the block of M Emory pointed by PTR to the current position in the stream.

The position indicator of the "stream is advanced by" total number of bytes written.

Internally, the function interprets the block pointed by PTR as if it is an array of (Size*count) elements of type uns Igned Char, and writes them sequentially to stream as if FPUTC is called for each byte.

fseek
int fseek (FILE * stream, long int offset, int origin);
Reposition Stream Position Indicator Sets The position indicator with the stream to a new associated.

For streams open in binary mode, the new position was defined by adding offset to a reference position of specified by origin .

For streams open in text mode, offset shall either is zero or a value returned by a previous call to Ftell, and origin shall necessarily be seek_set.

If The function is called with other values for this arguments, support depends on the particular system and library Impl Ementation (non-portable).

The End-of-file internal indicator of the The stream is cleared after a successful call to this function, and all effects fr Om previous calls to ungetc on the this stream are dropped.

On streams open for Update (read+write), a call to Fseek allows to switch between reading and writing.

ParametersStream pointer to a FILE object that identifies the stream. Offset Binary files:number of bytes to offset from origin.
Text Files:either Zero, or a value returned by Ftell. Origin Position used as reference for the offset. It is specified by one of the "following constants defined in <cstdio>exclusively to" used as arguments for this F Unction:
Constant Reference Position
Seek_set Beginning of File
Seek_cur Current position of the file pointer
Seek_end End of File *
* Library implementations are allowed to don't meaningfully support seek_end (therefore, code using it has no real standard Portability).

Ftell

Long int Ftell (FILE * stream);
Get-position in stream Returns the current value of the position indicator of the stream.

For binary streams, this is the number of bytes from the beginning of the file.

For text streams, the numerical value may is meaningful but can still is used to restore the position to the same tion later using fseek (if there are characters put back using ungetc still pending of being read, the behavior is undef ined).

reference materials

http://www.cplusplus.com/

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.