C Language Learning 010: fopen reading and writing files, language learning fopen

Source: Internet
Author: User

C Language Learning 010: fopen reading and writing files, language learning fopen

In the input.csv file, we have the following data:

Apple
Pear 
Litchis
Pineapple
Watermelon

Now we can read the input.csvfile and write it to the output.csv file. We will use the fopen function.

Function prototype: FILE * fopen (const char * path, const char * mode)   1 #include <stdio.h>   2 #include <stdlib.h>   3 #include <string.h>   4   5 int main () {   6 char line [80];   7 FILE * in = fopen ("input.csv", "r"); // fopen can create a data stream; r, read   8 FILE * out = fopen ("output.csv", "a"); // a, means append data to a file   9 while (fscanf (in, "% 79 [^ \ n] \ n", line) == 1) { 10 fprintf (out, "from input:% s \ n", line); 11} 12 // After running out of data streams, they need to be closed, even if they will shut themselves down, because usually a process can have a maximum of 256 data streams, the number is limited 13 fclose (in); 14 fclose (out); 15 return 0; 16}


Fopen has many other modes, such

W. Write the file. If the file does not exist, create the file and write it. If the file exists, overwrite the previous data.

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


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.