Analysis of C language file operations (IV)

Source: Internet
Author: User
Tags rewind

In addition to open and read/write operations, file operations also have several common operations. The following describes the functions involved in these operations.

I. Function for moving the position pointer

The rewind and fseek functions are prototype:

Void rewind (FILE * fp); move the position pointer to the beginning of the FILE

Int fseek (FILE * fp, long int offset, int origin); move the position pointer to the offset byte number from the origin

For parameters in the fseek function, the origin is the starting point, and the offset is the number of offset bytes from the origin.

There are three origin values: SEEK_SET (0)-> first file, SEEK_CUR (1)-> current location, SEEK_END (2)-> end of the file.

Note: 1) if the file is opened in append mode, the two functions do not work during write operations, no matter where the position pointer is moved, always append the added data to the end of the file.

 

Ii. Other common functions

1. ftell Function

Long int ftell (FILE * fp );

Calculates the number of bytes from the beginning of the file at the current position. If an error occurs,-1L is returned.

The ftell function can be used to calculate the file size.

2. feof Function

Int feof (FILE * fp );

Checks whether the pointer at the current position reaches the end of the file. If the pointer reaches the end of the file, a non-zero value is returned; otherwise, 0 is returned.

3. ferror Function

Int ferror (FILE * fp );

Checks whether an error occurs during file operations. If an error occurs, a non-zero value is returned. Otherwise, 0 is returned.

4. remove Function

Int remove (const char * filename );

If the object is deleted successfully, 0 is returned; otherwise, a non-zero value is returned.

5. rename Function

Int rename (const char * oldname, const char * newname );

Rename the file. If the name is successfully renamed, 0 is returned. Otherwise, a non-zero value is returned.

6. freopen Function

FILE * freopen (const char * filename, const char * mode, FILE * stream );

Implement redirection input and output. This function is used much in testing data.

7. fclose Function

Int fclose (FILE * stream );

If a stream is closed successfully, 0 is returned; otherwise,-1 is returned. Note that the stream must be closed after each file operation; otherwise, data may be lost.

Test procedure:

#include<stdio.h>#include<stdlib.h>int main(void){    freopen("input.txt","r",stdin);    freopen("output.txt","w+",stdout);    int i;    int a[10];    for(i=0;i<10;i++)    {        scanf("%d",&a[i]);    }    for(i=0;i<10;i++)    {        printf("%d\n",a[i]);    }    return 0;}

The hypothetical project directory contains input.txt, and the data in the file is 1 2-1 3 4 5 7 8 9 records. The results are not directly output to the console.

Author: Hai Zi

Related Article

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.