Stream-based file IO

Source: Internet
Author: User

Example: copy a file to another file. The hello.txt file must contain text before you can see the result.

 1 #include<stdio.h> 2 #include<stdlib.h> 3  4 int main(void) 5 { 6     FILE *fp1, *fp2; 7     int c; 8  9     if((fp1=fopen("hello.txt","rb"))==NULL)10     {11         printf("error to read\n");12         exit(1);13     }14     if((fp2=fopen("desk.txt","wb"))==NULL)15     {16         printf("fails to open file 2");17         exit(1);18     }19 20     while((c=fgetc(fp1))!=EOF)21     {22         if(fputc(c,fp2)==EOF)23         {24             printf("error to print\n");25             exit(1);26         }27     }28     fclose(fp1);29     fclose(fp2);30     return 0;31 }

We can see that, basically

To open a text file, use:

Fopen () function,

Use fclose () to close text; Use fgetc to get text from text

Use fputs to write text to the stream. Therefore, fp2 can also be stdout, and the file content will be written to the standard output;

 

1.

Character reading

#include<stdio.h>int getc(FILE *fp);int fgetc(FILE *fp);int getchar(void);

When getchar is used, the EOF signal can be sent through Ctrl + D.

 

2.

Character output

#include<stdio.h>int putc(int c,FILE *fp);int fputc(int c,FILE *fp);int putchar(void);

The definitions of putchar and getchar are similar.

 

3.

Line-based Io

Input:

#include<stdio.h>char *fgets(char *buf,int n,FILE *fp);char *gets(char *buf);

The return value is the first address of the buffer, n is the number of characters to read, and gets is read from the standard input.

Note: fgets will not read when \ n is encountered. Ensure that the last bit is '\ 0'. That is to say, at most n-1 characters are read in the last buffer, and the last bit is \ 0.

 

The gets function is a dangerous function. It is warned in GCC and is not recommended.

Because gets does not check the number of inputs, it may cause overflow and cause program crash.

 

Output:

#include<stdio.h>int fputs(const char *buf,FILE *restrict fp);int puts(const char *str);

Restrict is the new standard of c99, which indicates pointer access to the restricted address. That is, only the FP pointer can be used to access this address. Other pointers to this address are invalid.

Puts is not highly secure, so it is not recommended.

 

4.

Direct Io

#include<stdio.h>size_t fread(void *ptr,size_t size,size_t nmemb,FILE *fp);size_t fwrite(const void *ptr,size_t size, size_t nmemb,FILE *fp);

 

These two functions can be used to read and write blocks and objects.

Write part of the array into the stream

if(fwrite(&data[2],sizeof(float),4,fp)!=4)    {        printf("error");        exit(1);    }

Write structure to file

    struct    {        short count;        long total;        char name[20];    }item;    if(fwrite(&item,sizeof(item),1,fp)!=1)    {        printf("error\n");    }

In combination, you can read and write structural arrays.

A typical application is reading and writing address books.

 

The following is an example of copying a file using direct IO:

 1 #include<stdio.h> 2 #include<stdlib.h> 3  4 #define SRC "hello.txt" 5 #define DES "des.txt" 6  7 int main(void) 8 { 9     FILE *fp1,*fp2;10     char buf[1024];11     int n;12     if((fp1=fopen(SRC,"rb"))==NULL)13     {14         printf("error 1\n");15         exit(1);16     }17     if((fp2=fopen(DES,"wb"))==NULL)18     {19         printf("error 2\n");20         exit(1);21     }22     while((n=fread(buf,sizeof(char),1024,fp1))>0)23     {24         if(fwrite(buf,sizeof(char),n,fp2)==-1)25         {26             printf("error 3\n");27             exit(1);28         }29     }30     if(n==-1)31     {32         printf("error 4\n");33         exit(1);34     }35     fclose(fp1);36     fclose(fp2);37     return 0;38 }

 

5.

Format Io

#include<stdio.h>int printf(const char *format,...);int fprintf(FILE *fp,const char *format,...);int sprintf(char *str,const char *format,...);int snprintf(char *str,size_t size,const char *format);//maybe only in Linuxint scanf(const char *format,...);int fscanf(FILE *fp,const char *format,...);int sscanf(char *str,const char *format,...);

 

For other formatting methods, see other articles.

Stream-based file 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.