C Language File operation analysis and example code

Source: Internet
Author: User
Tags rewind
C Language File Operation parsing

There are several more common operations in file operations, in addition to opening operations and reading and writing operations. The functions involved in these operations are described below.

I. Functions for moving position pointers

The rewind function and the fseek function, the prototypes of these two functions are:

void Rewind (FILE *fp); Move the position pointer to the top of the file

int fseek (FILE *fp,long int offset,int origin); Move the position pointer to the location of the offset byte number from Origin

Where the parameter in the Fseek function, origin is the starting point, offset is the number of bytes from Origin

Origin has a value of three: Seek_set (0), file header, Seek_cur (1), current position, Seek_end (2), end of file.

Note: 1) If the file is opened in append mode, the two functions do not work when the write operation, regardless of where the position pointer is moved, always append the added data to the end of the file.

Two. Other common functions

1.ftell function

Long int ftell (FILE *fp);

Calculates the number of bytes of the current position pointer from the beginning of the file, and returns -1L if an error occurs.

Use the Ftell function to calculate the size of the file.

2.feof function

int feof (FILE *fp);

Detects whether the current position pointer reaches the end of the file, returns a value other than 0 if the end of the file is reached, or returns 0.

3.ferror function

int ferror (FILE *fp);

Detects if an error occurred during file operation, returns a value other than 0 if an error occurs, or returns 0

4.remove function

int remove (const char *filename);

Delete file, if delete succeeds, return 0, otherwise return non 0 value

5.rename function

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

Renaming the file, renaming succeeds returns 0, otherwise a non-0 value is returned.

6.freopen function

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

Implement redirect input and output. This function is used more when testing data.

7.fclose function

int fclose (FILE *stream);

Closes a stream, returns 0 if successful, otherwise returns-1. Note that you need to close the stream every time you finish working on the file, or you may lose data.

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;}

Assuming that the input.txt exists in the project directory, the data in the file is 1 2-1 3 4 5 7 8 9 10, then after running, you do not need to input data from the console, the program reads the data directly from the Input.txt, and then outputs the results to output.txt without directly outputting the results to the console.

Thank you for reading, hope to help everyone, more relevant articles please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.