Basic operation of C language for files

Source: Internet
Author: User
Tags fread

In the C language, the operation of the file is made by using the document structure body.

Introduction to several commonly used operations file functions

1: Open file *fopen (const char *filename, const char *mode);

The first parameter is a pointer type to the file name string constant;

The second parameter specifies the mode in which the file opens.

File Open mode:

R: Read, if the file does not exist, the function call fails;

W: Open an empty file for the write operation. If the file does not exist, a file is created, and if the given file already exists, its contents will be emptied;

A: Open the file for the write operation. If the file does not exist, a file is created first, and if the file exists, new data is added at the end of the file, and the existing EOF tag is not removed until the data is written;

r+: Open file for write operation and read operation, file must exist;

w+: Write and read, other with W;

A +: Open file for read and add operations, other same as a.

Note: After opening the file will usually do some file read or write operations, if the file open failed, the next read and write action can not be carried out smoothly, so generally in fopen () after the error judgment and processing.

[CPP]View PlainCopy
    1. #include <stdio.h>
    2. Main ()
    3. {
    4. FILE * Pfile=fopen ("ZKD","R");
    5. if (pfile==null) return;
    6. Fclose (PFile);
    7. return 0;
    8. }

2: file writes size_t fwrite (const void *buffer, size_t size, size_t count, file *stream)

First parameter: Point to the data that will be written to the file;

Second parameter: The size of the item in bytes. size_t type, in fact, is unsigned int type;

Third parameter: The maximum number of items to be written;

Note: The Write data size is guaranteed to be the second parameter and the product of the third parameter.

The fourth parameter: a pointer to the file type, obtained by fopen.

3: Flush buffer fflush (FILE *stream)

The C language operation of the file uses a cache file system, that is, the system automatically opens up a cache area for each file in use in memory, and the data written from the memory to the disk file is sent to the buffer in memory until the buffer's data is full before the data is sent to the disk file.

 FILE *pfile=fopen ( " zkd.txt   ", "  w   " );  if  (Pfile==null)  return  1  ; Fwrite (  " i love you!   ", 1 , strlen (  " i love you!   "      

4: Closing of files fclose (file *stream )

Closes the file, and the buffer contents are also written to disk, but the next time you want to read and write to the file, you must open the file again, as opposed to fflush.

5: file pointer positioning int fseek (file *stream, long offset, int origin);

In the C-language file operation, there will be a file pointer, the pointer will be at any time according to our action on the file to move the location, always point to the next person to write the location;

Second parameter: offset (compared to the position of the file pointer)

Third parameter: Specifies the starting position of the file pointer, which can fetch three values:

Seek_cur: Start at the current location of the file;

Seek_end: Start at the end of the file;

Seek_set: Start at the beginning of the file.

6: file read size_t fread (void *buffer, size_t size, size_t count, file *stream)

First parameter: Point to the buffer that holds the data;

Other the same fwrite ();

7: Get file Length method long ftell (file *stream)

function Ftell The number of offset bytes from the current position of the file position pointer relative to the top of the file

Use the Fseek function to move the file pointer to the end of the file, and then use the Ftell function to get the length of the file.

Fseek (Pfile,0,seek_end);

int Len=ftell (pFile);

Len is the file length

#include"stdafx.h"#include<stdio.h>#include<string.h>#include<stdlib.h>int_tmain (intARGC, _tchar*argv[]) {FILE*pfile=fopen ("Zkd.txt","R"); if(pfile==NULL)return 1; Fwrite ("I Love you!",1, strlen ("I Love you!"), pFile);                      Fflush (PFile); Fseek (PFile,0, Seek_end); intlen=Ftell (PFile); Char*ch= (Char*) malloc (len+1); //memset (ch,0,100); Rewind (pFile); Fread (CH,1, Len,pfile); Ch[len]=' /'; printf ("%s", CH);                      Fclose (PFile); return 0; } 

Note the point:

File opening and closing is a companion, there is a file open there is a file close.

When you read and write files, you need to be aware of the file location pointer , the file location pointer to control where the file reads and writes, such as whether to read or write at the beginning or the last. For example, when we call FGETC () to read a character, the file position pointer will automatically point to the next byte.

The file pointer and the file location pointer are two different concepts, the file pointer points to the entire file structure, and the file location pointer points to where the file reads and writes.

Reference article: 18269989

Basic operation of C language for files

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.