The use of fread and fseek

Source: Internet
Author: User
Tags fread

Original: http://baike.baidu.com/view/656696.htm

Http://baike.baidu.com/view/656689.htm

fread function: Read data function prototype from a stream: size_t fread ( void *buffer, size_t size, size_t count, FILE *Stream     ); Parameters: 1. Address (pointer) (buffer) for receiving data 2. The size of a single element: The unit is a byte instead of a bit, for example reading an integer is 2 bytes 3. Number of elements (count) 4. Provide data The file pointer (stream) return value: Number of elements successfully read

Program Examples

#include <stdio.h>

int main (void)
{
FILE *stream;
Char msg[] = "This is a test";
Char buf[20];
if (stream = fopen ("Dummy.fil", "w+") = = = NULL)
{
fprintf (stderr, "Cannot open output file.\n");
return 1;
}
Fwrite (Msg,strlen (msg) +1,1,stream);
Fseek (Stream,0,seek_set);
Fread (Buf,strlen (msg) +1,1,stream);
printf ("%s\n", buf);
Fclose (stream);
return 0;
}

Fseek

Directory

function
usage
description
return value
Program Example
considerations
function

Reposition the file's internal position pointer on the stream (data stream/file) Note: Not a positional file pointer, the file pointer points to a file/stream. The position pointer points to the byte position inside the file, and as the file's read moves, the file pointer does not change to the other file if it is not re-assigned.

usage

int fseek (FILE *stream, long offset, int fromwhere);

Description

The function sets the location of the file pointer stream. If successful, the stream will point to the position of offset (pointer offset) byte at the base of Fromwhere (offset start position: file header 0, current position 1, end of File 2). If execution fails (for example, offset exceeds the size of the file itself), the location pointed to by the stream is not changed.

return value

Succeeds, returns 0, otherwise returns the other value.

Fseek position the file position (position) pointer (pointer) for the file referenced by stream to the byte site calculated by Offset.

int fseek (FILE *stream, long offset, int origin);
The first parameter stream is a file pointer
The second parameter offset is offset, positive number indicates positive offset, negative is negative offset
The third parameter, origin, sets the offset from where the file begins, possibly with a value of: Seek_cur, Seek_end, or Seek_set
Seek_set: File Start
Seek_cur: Current Location
Seek_end: End of File
Where Seek_set,seek_cur and Seek_end are 0, 1, and 2 in turn.
Simply:
Fseek (fp,100l,0); Move the internal pointer of the file to 100 bytes from the beginning of the file;
Fseek (fp,100l,1); Move the internal pointer of the file to 100 bytes from the current position of the file;
Fseek (fp,-100l,2); Returns the internal pointer to the file at 100 bytes from the end of the file.

The use of fread and fseek

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.