String I/O: fgets and fputs

Source: Internet
Author: User

Function Prototypes:
Char *fgets (char *s,int n,file *FP)
int fputs (char *s,file *FP)

Function: Read/write a string from the FP pointing to the file
return value:
Fgets returns the first address of the read string when normal, error or end of file, return null
Fputs returns the last character written when normal; error is EOF

Fputs writes the string that s points to the file that the FP points to
Fgets reads n-1 characters from the FP file into the memory area pointed to by S.
And at the end add a ' plus '
(Ends when a newline character or end of file (EOF) is encountered before n-1 characters are read in.)

Example reads a string from the keyboard into a file, and then reads back from the file display

#include <stdio.h>Main () {FILE *fp;Char  string[Bayi];if((Fp=fopen ("File.txt","W")) ==null) {printf("Cann ' t Open file");Exit(0); } while(strlen(Gets (string)) >0)    {fputs(string, FP);fputs("\ n", FP); } fclose (FP);if((Fp=fopen ("File.txt","R")) ==null) {printf("Cann ' t Open file");Exit(0); } while(Fgets (string,Bayi, FP)!=null)fputs(string, stdout); Fclose (FP);}

String I/O: fgets and fputs

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.