Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use fputs () and fgets () functions in C language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use the fputs () and fgets () functions in the C language. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Write file: the fputs () function fputs () function writes a line of string to the file, which outputs the string to the stream.

It behaves as follows:

(1) return if you encounter a line break or an EOF at the end of the file.

(2) read by line.

(3) the newline character'/ n' at the end of each line is also a line character.

(4) for buf with the size of size, only size-1 characters can be read at most.

(5) automatically populate the last byte of the last character (usually a newline character) in the buf with the zero Terminator ('/ 0').

The syntax of the fputs () function:

Int fputs (const char * s, FILE * stream)

Example: create a source file: fputs-write-file.c, whose source code is as follows-

# include//20200427void main () {FILE * fp;fp = fopen ("my-write-file.txt", "w"); fputs ("hello c programming\ n", fp); fputs ("performance c programming\ n", fp); printf ("all content had write to file: my-write-file.txt\ n"); fclose (fp) } execute the above sample code and get the following result: all content had write to file: my-write-file.txt after executing the above code, open the file: my-write-file.txt, you should see the following content-hello c programmingperformance c programming

Read the file: the fgets () function fgets () function reads a line of string from the file, which fetches the string from the stream.

It behaves as follows:

(1) enter all the text before the zero Terminator in str into the file.

(2) after the input is completed, no additional special characters, such as newline characters, will be added.

Syntax:

Char* fgets (char* s, int n, FILE * stream)

Example: create a source file, fgets-read-file.c, whose code is as follows:

# include//20200427void main () {FILE * fp; char text [300]; fp = fopen ("my-write-file.txt", "r"); printf ("% s", fgets (text, 200, fp)); / / first line printf ("% s", fgets (text, 200, fp)); / / second line fclose (fp);} execute the above example code to get the following result: hello c programmingperformance c programming

On the C language fputs () and fgets () function how to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report