Indispensable Windows Native (10), windowsnative

Source: Internet
Author: User
Tags rewind

Indispensable Windows Native (10), windowsnative

[Download source code]

Indispensable Windows Native (10)-C language: File



Author: webabcd


Introduction
Indispensable C language for Windows Native

  • File



Example
CFile. h

/ *
 * Document
 *
 * From the user's perspective, files can be divided into ordinary files and device files
 * 1. Ordinary file refers to a piece of data stored on a disk or other external media
 * 2. Device files refer to various external devices connected to the host. The operating system will also treat various external devices as a file for management. For example, the monitor is usually defined as a standard output file, and the keyboard is defined as a standard input file.
 *
 * From the perspective of file encoding, files can be divided into ASCII files (text files) and binary files
 * 1. The ASCII file is also called a text file. When this file is stored on a disk, each character corresponds to one byte and is used to store the corresponding ASCII code. For example: the string 1234567 is stored as a text file by characters, occupying 7 bytes
 * 2. Binary files are stored in binary encoding. For example: the number 1234567 is stored as a binary file as an integer, occupying 4 bytes
 *
 * About mkdir (make directory)-create a directory, rmdir (remove directory)-delete a directory and other functions, write later
 *
 *
 * Note: EOF (end of file)-end of file
 * /

#include "pch.h"
#include "cFile.h"
#include "cHelper.h"

void file_demo1 ();
void file_demo2 ();
void file_demo3 ();
void file_demo4 ();
void file_demo5 ();

char * _rootPath;

char * demo_cFile (char * rootPath)
{
    _rootPath = rootPath;

    // write to text file
    file_demo1 ();

    // read text file
    file_demo2 ();

    // Read and write text files in a formatted manner
    file_demo3 ();

    // read and write binary files
    file_demo4 ();

    // Internal positioning of the file (random read and write, that is, read and write anywhere)
    file_demo5 ();

    return str_concat2 ("Saving path for the demo file:", rootPath);
}


// write to text file
void file_demo1 ()
{
    // file pointer-pointer to file (FILE * pointer variable identifier)


    // file path to be accessed
    char * fileName = str_concat2 (_rootPath, "\\ c_file_demo1.txt");

    // fopen-opens the file at the specified path in the specified way and returns a file pointer (FILE is a structure defined by the system, fp is a file pointer to FILE)
    FILE * fp = fopen (fileName, "wt +"); // The second parameter is the file opening method. For the description, see the following note
    if (fp == NULL)
    {
        // If fopen returns a null pointer, the file failed to open (the last error is stored in errno)
        int errNum = errno; // see errno.h for pre-defined errno
    }
    else
    {
        // rewind-move the internal pointer of the specified file pointer to the file header
        rewind (fp);

        // fputc-write characters to the specified file (here EOF means write failed)
        if (fputc ('w', fp) == EOF)
        {
            // ferror-check if the file has errors during reading and writing. A return value of 0 indicates normal, otherwise it indicates an error.
            int errNum = ferror (fp); // See errno.h for predefined errno

            // clearerr-Clears the error flag to a value of 0.
            clearerr (fp);
        }

        // fputs-write a string to the specified file (here EOF means write failed)
        if (fputs ("ebabcd \ nwanglei", fp) == EOF)
        {
            // ferror-check if the file has errors during reading and writing. A return value of 0 indicates normal, otherwise it indicates an error.
            int errNum = ferror (fp); // See errno.h for predefined errno

            // clearerr-Clears the error flag to a value of 0.
            clearerr (fp);
        }

        // fclose-After opening the file, you must use fclose to close the file. If 0 is returned, the file is closed normally.
        int fcloseResult = fclose (fp);
    }

    free (fileName);
}


// read text file
void file_demo2 ()
{
    char * fileName = str_concat2 (_rootPath, "\\ c_file_demo1.txt");

    FILE * fp = fopen (fileName, "rt");
    if (fp == NULL)
    {
        int errNum = errno;
    }
    else
    {
        rewind (fp);

        // fgetc-read character by character. If EOF is encountered, the file is read or error occurs (the end of each file has an end flag-EOF)
        char ch = fgetc (fp);
        while (ch! = EOF)
        {
            // Every time a character is read, the internal position pointer moves backward by one byte
            ch = fgetc (fp);
        }

        // feof-check if the file ends (that is, if the internal position pointer points to EOF), if it ends, return a non-zero value, otherwise return 0
        int isEnd = feof (fp); // non-zero value

        rewind (fp);

        // feof-check if the file ends (that is, if the internal position pointer points to EOF), if it ends, return a non-zero value, otherwise return 0
        isEnd = feof (fp); // 0

        / *
         * fgets (str, n, fp)-read string function
         * str-the character array name, because the read string is saved
         * n-read n-1 characters and send them to the character array str, then add '\ 0' to the end (before reading n-1 characters, if a newline or EOF is encountered, the reading ends)
         * fp-file pointer
         * /
        char str [32];
        while (fgets (str, 32, fp)! = NULL)
        {
            // This example will loop twice
            // The result of the first str is: webabcd \ n
            // The result of the second str is: wanglei
        }

        fclose (fp);
    }

    free (fileName);
}


// Read and write text files in a formatted manner
void file_demo3 ()
{
    char * fileName = str_concat2 (_rootPath, "\\ c_file_demo2.txt");

    FILE * fp = fopen (fileName, "wt +");
    if (fp == NULL)
    {
        int errNum = errno;
    }
    else
    {
        // fprintf (file print formatted)-write a string in a formatted way (return value is the number of bytes written)
        int fprintfResult = fprintf (fp, "% s% d% d \ n", "webabcd", 100, 20); // 15

        rewind (fp);

        int num;
        int age;
        char name [32];

        // fscanf (file scan formatted)-read the string in a formatted way (the return value is the number of members read)
        int fscanfResult = fscanf (fp, "% s% d% d \ n", name, & num, & age); // 3

        fclose (fp);
    }

    free (fileName);
}


// read and write binary files
void file_demo4 ()
{
    struct employee
    {
        int num;
        char name [32];
    } w [2] = {{100, "wanglei"}, {200, "webabcd"}}, r [2], * ww, * rr;

    ww = w;
    rr = r;

    char * fileName = str_concat2 (_rootPath, "\\ c_file_demo3.b");

    FILE * fp = fopen (fileName, "wt +");
    if (fp == NULL)
    {
        int errNum = errno;
    }
    else
    {
        / *
         * fwrite (buffer, size, count, fp)-write binary data (return value is the number of data blocks successfully written)
         * buffer-pointer to the data to be written
         * size-the number of bytes per data block
         * count-the number of data blocks to be written
         * fp-file pointer
         * /
        int fwriteResult = fwrite (ww, sizeof (struct employee), 2, fp); // 2

        rewind (fp);

        / *
         * fread (buffer, size, count, fp)-read binary data (return value is the number of data blocks successfully read)
         * buffer-pointer to hold the read data
         * size-the number of bytes per data block
         * count-the number of data blocks to be read
         * fp-file pointer
         * /
        int freadResult = fread (rr, sizeof (struct employee), 2, fp);

        fclose (fp);
    }

    free (fileName);
}


// The internal positioning of the file (random read and write, that is, random read and write)
void file_demo5 ()
{
    struct employee
    {
        int num;
        char name [32];
    } w [2] = {{100, "wanglei"}, {200, "webabcd"}}, r [1], * ww, * rr;

    ww = w;
    rr = r;

    char * fileName = str_concat2 (_rootPath, "\\ c_file_demo4.b");

    FILE * fp = fopen (fileName, "wt +");
    if (fp == NULL)
    {
        int errNum = errno;
    }
    else
    {
        int fwriteResult = fwrite (ww, sizeof (struct employee), 2, fp);

        / *
         * rewind (fp)-move the pointer inside the file to the file header
         * fp-file pointer
         * /
        rewind (fp);

        / *
         * fseek (fp, offset, origin)-move the pointer inside the file
         * fp-file pointer
         * offset-the number of bytes relative to the origin (long integer, remember to add "L" if using a constant)
         * origin-the starting point of the displacement
         * SEEK_SET (0)-file header
         * SEEK_CUR (1)-current position
         * SEEK_END (2)-end of file
         * /
        fseek (fp, sizeof (struct employee), SEEK_SET);

        int freadResult = fread (rr, sizeof (struct employee), 1, fp); // 200, "webabcd"

        fclose (fp);
    }

    free (fileName);
}



/ *
 * A description of the file opening method, which is the second parameter of fopen
 * rt read-only open a text file, only read data is allowed
 * wt only write open or create a text file, only write data is allowed
 * at Append to open a text file and write data at the end of the file
 * rb read-only opens a binary file, only read data is allowed
 * wb only write open or create a binary file, only write data is allowed
 * ab Append to open a binary file and write data at the end of the file
 * rt + read and write opens a text file, allowing reading and writing
 * wt + read and write opens or creates a text file, allowing reading and writing
 * at + Read and write opens a text file, allowing reading, or appending data at the end of the file
 * rb + read and write opens a binary file, allowing reading and writing
 * wb + read and write opens or creates a binary file, allowing reading and writing
 * ab + read and write open a binary file, allow reading, or append data at the end of the file
 *
 * About the above characters
 * r (read) read
 * w (write) write
 * a (append) append
 * t (text) text file, can be omitted without writing
 * b (banary) binary
 * + Read and write
 * /



OK
[Download source code]


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.