C language Input and output function fputs (...)

Source: Internet
Author: User

C language fgets (...) Read input from stream, opposite fputs (...) Writes data to a file.

for ANSI C programs, the runtime system opens at least three streams, including the following 3 streams:

1. Standard input standard inputs. The standard definition is stdin.

2 standard output standard outputs. Standard defined as stdout

3. Standard error Standard. The standard is defined as stderr.

at the same time, use the file structure to point to these three streams.

fputs (...) Used to write data to these three streams.

prototype int fputs (char *s, FILE *stream);

S is a character pointer, which can be either a character array or a character pointer, or you can use a string constant as a parameter.

Example:

FILE *fp=fopen ("Test.txt", "w");

char s1[20]= "Hello World";

char *s2= "Hello C";

fputs (S1,FP); Array name

fputs (S2,FP); Character Pointer

fputs ("Hello", FP); String Constants

The above three usages are all OK, in essence, the direct value of string in C language is actually a pointer.

return Value:

If the write succeeds, a non 0 is returned, at which point the compiler defaults to return 1.

If an error is written, EOF is returned.

Note: Once the fputs (char *s, file *stream) function writes data to a file, the file position pointer automatically moves backwards.

fputs (...) Output data to the screen.

Since the file structure can point to three streams, you can point to the stdout stream as well

so:

fputs ("Hello World", stdout);

just want the screen to output Hello Word.

Finally, look at the fputs (...) The standard library implementation of the function:

int fputs (char *s, FILE *stream)

  {      

int C;

while (c =*s++)//From here you can see that fputs does not write to the stream the null character at the end of the string.

PUTC (c,stream);

return ferror (stream)? EOF: Non-negative

 }     

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.