Operating instance of file I/O

Source: Internet
Author: User

1_OPEN.C:

#include <sys/types.h>#include<stdio.h>#include<sys/stat.h>#include<fcntl.h>intMainintargcChar*argv[]) {    intFd//File Descriptor        /*open a file as read and return an error if the file does not exist*/FD= Open ("Test.txt", o_rdonly); if(FD <0) {perror ("Open"); return-1; } printf ("FD = =%d\n", FD); return 0;}

2_OPEN.C:

#include <sys/types.h>#include<stdio.h>#include<sys/stat.h>#include<fcntl.h>intMainintargcChar*argv[]) {    intFd//File Descriptor        /*open a file as read and create it if the file does not exist*/FD= Open ("Test.txt", O_rdonly | O_creat,0755); if(FD <0) {perror ("Open"); return-1; } printf ("FD = =%d\n", FD); return 0;}

3_READ.C:

#include <sys/types.h>#include<stdio.h>#include<sys/stat.h>#include<fcntl.h>intMainintargcChar*argv[]) {    intFd//File Descriptor    intret; Charbuf[ -] = {0}; /*Read Standard terminal: 0 (Stdin_fileno)*/ret= Read (0, BUF,sizeof(BUF)-1); if(Ret >0){        /*Read Success*/        /*Print to Terminal*/printf ("%s\n", BUF); }    return 0;}

4_READ.C:

#include <sys/types.h>#include<stdio.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>intMainintargcChar*argv[]) {    intFd//File Descriptor    intret; Charbuf[ -] = {0}; /*open a file to read the way*/FD= Open ("Test.txt", o_rdonly); if(FD <0) {perror ("Open Test"); return-1; }        /*move the file pointer to the file header*/Lseek (FD,0, Seek_set); /*Read Standard terminal: 0 (Stdin_fileno)*/ret= Read (Fd,buf,sizeof(BUF)-1); if(Ret >0){        /*Read Success*/        /*Print to Terminal*/printf ("%s\n", BUF); }    return 0;}

5_WRITE.C:

#include <sys/types.h>#include<stdio.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>intMainintargcChar*argv[]) {    intFd//File Descriptor    intret; Charbuf[ -] = {0}; /*open a file to read the way*/FD= Open ("Test.txt", o_rdonly); if(FD <0) {perror ("Open Test"); return-1; }        /*move the file pointer to the file header*/Lseek (FD,0, Seek_set); /*Read Standard terminal: 0 (Stdin_fileno)*/ret= Read (Fd,buf,sizeof(BUF)-1); if(Ret >0){        /*Read Success*/        /*Print to Terminal*/Write (1, Buf,ret); }    return 0;}

6_WRITE_READ.C:

#include <sys/types.h>#include<stdio.h>#include<sys/stat.h>#include<fcntl.h>#include<unistd.h>intMainintargcChar*argv[]) {    intFdr//File Descriptor    intFDW; intret; Charbuf[ -] = {0}; /*open a file to read the way*/FDR= Open ("Test.txt", o_rdonly); if(FDR <0) {perror ("Open Test"); return-1; }        /*Open a file dest.txt, for write, if the file does not exist, create*/FDW= Open ("Dest.txt", O_wronly | O_creat,0755); if(FDW <0) {perror ("Open Dest"); return-1; }    /*move the file pointer to the file header*/Lseek (FDR,0, Seek_set); /*Read Standard terminal: 0 (Stdin_fileno)*/ret= Read (Fdr,buf,sizeof(BUF)-1); if(Ret >0){        /*Read Success*/        /*write to File Dest.txt*/write (Fdw,buf,ret); }    return 0;}

Operating instance of file I/O

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.