C language Learning 010:fopen read and write files

Source: Internet
Author: User

In the file Input.csv file, we have the following data

Applepear Litchispineapplewatermelon

Now we will read and write to the Output.csv file under the Input.csv file, we will use the fopen function

  Function Prototypes: FILE * fopen (const char * path,const char * mode)

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 5 intMain () {6     Charline[ the];7FILE *inch=fopen ("Input.csv","R");//fopen can create a data stream; R, which indicates the read8FILE * out=fopen ("Output.csv","a");//A, indicating append data to the file9      while(FSCANF (inch,"%79[^\n]\n", line) = =1){Tenfprintf ( out,"From input:%s\n", line); One     } A     //after you run out of data streams, you need to close them, even if they are shut down, because normally a process can have up to 256 data streams, and the number is limited -Fcloseinch); -Fclose out); the     return 0; -}

Fopen also has many patterns, such as

W, writes the file, if the file does not exist, creates the file and writes it, if the file exists, overwrites the previous data

There are a+,w+,r+ and so on, but some compilers do not support, you can refer to here fopen

C language Learning 010:fopen read and write files

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.