File IO and standard IO

Source: Internet
Author: User
Tags posix

2015.2.26

Thursday, Cloudy

Today's content is primarily file IO

The Manual of Man is in fascicle:

Man-f Open View those fascicle with open
Man 1--General Command program
Man 2-System call
Man 3--Library function

File: The operating system abstracts hardware into files
Input: Writes data from the device into memory
Output: Write out the in-memory data to the device

The Linux file system consists of two layers: the first layer is the virtual file system (VFS), the second layer is a variety of specific file systems

POSIX: Portable Operating System Interface Specification
API: User Programming interface

Applications are made system calls via POSIX and GNU C LIB (System accessible): called this interface as a library function

Application Direct Access System: System call

The upper layer to access the system requires a system call first,

Error handling

Strerror (): Map errno corresponding error message: Strerror (errno), error is global variable
Prerror (); Output user information and errno corresponding error message

printf ("File to Open:%s\n", Strerror (Error))
Perror ("File to open:")
The above two statements function like: file to open:no such file directory (later prompt statement and program function related)


Linux files are divided into 6 main types: Ordinary files, directory files, symbolic link files, pipeline files, socket files and device files.

IO without cache: File IO
IO with cache: Standard IO
Write out: Output
Read in: Input (relative to program)


Open (); Read (); write ():
Need to include the following header files
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

Standard IO: Encapsulates file descriptors and caching mechanisms

Fseek and Ftell = = Lseek;

Functions for File IO:
Open (), read (), write (), Lseek (), close ();

When the total number of bytes is greater than the number of bytes per read and write, there are two simple ways to judge whether or not to read and write.
1. Cumulative: (set total bytes =max)

while ((Nbyte = Write (fd, buf, N)) > 0)
{
sum + = Nbyte;
if (sum = = MAX)
{
Break
}
}

2. Interpret whether the actual number of bytes read and the number of read bytes set are equal

while (1)
{
i = Read (FD, buf, N);
Write (FD, buf, i);
if (i! = N)
{
Break
}
}

off_t lseek (int fd, off_t offset, int whence)

Offset: Offsets relative to base point whence, in bytes, positive for forward, negative for backward movement

Get the length of the file:
Length = Lseek (FD, 0, Seek_end)//notes several macros (Seek_set, seek_cur, Seek_end)

Standard IO;
fopen (), Fread (), fwrite (), fseek (), Ftell (), fprintf (), fclose ();

The length of the file: Fseek () and Ftell () Two function combination program function equivalent to Lseek ();
FILE *FP;
if (fseek (FP, 0, Seek_end) < 0)
{
Perror ("fseek error");
Fclose (FP);
RETRN 1;
}
Length = Ftell (FP);

Complete program for file length:

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>

int main (int atgc, char *argv[])
{
FILE *FP;
Long length;

if (ARGC < 2)
{
fprintf (stdout, "Usage:%s filename\n", argv[0]);
return 1;
}

if (fp = fopen (Argv[1], "r")) = = = NULL)
{
fprintf (stderr, "fopen Error:%s", Strerror (Erron));
return 1;
}

if (fseek (FP, 0, Seek_end) < 0)
{
Perror ("fseek error");
Fclose (FP);
return 1;
}

Lingth = Ftell (FP);

printf ("The Fiel size is%ld\n", length);

Fclose (FP);
return 0;
}


*************************************************************************************************************** ************************************************
*************************************************************************************************************** ************************************************
o************************************************************************************ *************************************************
*************************************************************************************************************** ************************************************

File IO and standard IO

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.