"Good Programmer's note sharing"--file operation

Source: Internet
Author: User

iOS training------My C language notes, look forward to communicating with you!

Today, the focus of the C language is basically the technical forget, but there is a final point of knowledge has to be mentioned, that is the file operation.

We all know that every day we use the computer, mobile phone, or other electronic or mobile devices, in fact, we are in use every moment in the implementation of the file operation of this different step, but the details of this we may be clear, then today I will tell you what is the C language of the file operation.

A: File operation introduction

File operations: Manipulating files through programs
READ: File-〉 memory
Write: Memory-〉 file

File is actually a collection of data stored on external media, and we are using the appropriate technology to let him achieve the results we want to achieve.

The program often has a large number of file input and output operations, which often constitute the main part of the program, so the C language provides a lot of input and output functions, which are used for two types of file input and output system: The ANSI standard defined by the buffer file (also known as standard file (stream) input and output (I/O) system) The other is a non-buffered file (also known as a non-standard file (stream) input/output (I/O) system) that is not defined in the ANSI standard.

You need to use a header file here:stdlib.h

Two: File operation application

1. Open a file


#include <stdio.h>
Main ()
{
File *FP = fopen () write a document first
FILE *FP; /* Define a file pointer */
int i;
fp=fopen("Wenjian", "RB"); /* Open the file with the current directory named Wenjian read-only */
if (fp==null)/* Determine if the file is open successfully */
Puts ("File open Error");/* Prompt Open unsuccessful */
i=fclose(FP); /* Close the Open file */
if (i==0)/* Determine if the file is closed successfully */
printf ("O,k"); /* Prompt to close success */
Else
Puts ("File close Error");/* Prompt Close unsuccessful */
}

2. Generate a Text.dat file after running


#include <stdio.h>
Main ()
{
Char *s= "That ' s good news"); /* Define a string pointer and initialize */
int i=617; /* Define integer variables and initialize */
FILE *FP; /* Define the file pointer */
Fp=fopne ("Test.dat", "w"); /* Create a text file to write only */
fputs ("Your score of Toeflis", FP);/* Writes a string of characters to the built file */
FPUTC (': ', FP); /* Write a colon to the file you built: */
fprintf (FP, "%d\n", I); /* Write an integer number to the file you built */
fprintf (FP, "%s", s); /* Write a string to the file you built */
Fclose (FP); /* Close File */
}

Three: summary of file operations

A batch of data in the form of files can be stored in the external media (mainly disk, CD-ROM, etc.), can also be read into the memory from the media. The operating system is a file-based management of the numbers. and the various C file systems can operate directly on their files.

Because the program often has a large number of file input and output operations, it often constitutes a major part of the program, so the C language provides a lot of input and output functions, which are used for two types of file input and output system: The ANSI standard defined by the buffer file (also known as standard file (stream) input and output (I/O) system) The other is non-buffered files (also known as non-standard file (stream) input and output (I/O) systems that are not defined in the ANSI standard.

Here are some of the most commonly used file manipulation functions for C:

    1. fopen () Open stream
    2. Fclose () Close the stream
    3. FPUTC () write a character into the stream
    4. FGETC () reads a character from the stream
    5. Fseek () navigates to the specified character in the stream
    6. Fputs () write String to stream
    7. Fgets () reads a line or a specified character from the stream
    8. fprintf () output to stream by format
    9. FSCANF () read by format from stream
    10. Feof () returns True when the end of the file is reached
    11. Ferror () returns its value when an error occurs
    12. Rewind () reset the file locator to the beginning of the file
    13. Remove () Delete file
    14. Fread () reads the specified number of characters from the stream
    15. Fwrite () writes a specified number of characters to the stream
    16. Tmpfile () generates a temporary file stream
    17. Tmpnam () generates a unique file name

"Good Programmer's note sharing"--file operation

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.