C Language File Operation Fgets ()/fputs ()

Source: Internet
Author: User

Say a fgets (..) Function.

Prototype char * fgets (char * s, int n,file *stream);

Parameters:

S: A character pointer that points to the address of the buffer that stores the read-in data.

N: n-1 characters are read from a stream

Stream: A stream that points to the read.

return value:

1. Returns NULL when N<=0, which is a null pointer.

2. When n=1, return the empty string "".

3. If the read-in succeeds, the address of the buffer is returned.

4. Returns null if a read-in error or a file end (EOF) is encountered.

Take a look at the official description of this function:

/***
*char *fgets (String, count, stream)-input string from a stream
*
*purpose:
* Get a string, up to count-1 chars or ' \ n ', whichever comes first,
* append ' + ' and put the whole thing into string. The ' \ n ' is included
* in the string. If count<=1 no input is requested. If EOF is found
* Immediately, return NULL. If EOF found after chars read, let EOF
* Finish the string as ' \ n ' would.
*
*entry:
* Char *string-pointer to place to store string
* int Count-max characters to place at string (include)
* FILE *stream-stream to read from
*
*exit:
* Returns string with the text read from file in it.
* If Count <= 0 return NULL
* if Count = = 1 put null string in string
* Returns NULL if error or end-of-file found immediately
*
*exceptions:
*
*******************************************************************************/

Fgets (...) in standard library Implementation of:

/****************************************************

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

{

register int C;

Register char *cs;

Cs=s;

while (--n>0 && (c = getc (stream))!=eof)

if ((*cs++= c) = = ' \ n ')

Break

*cs = ' + ';

return (c = = EOF && cs = = s)? Null:s;

}

/********************************************************

In the use of Fgets (..) When you read data, you define a character array or a character pointer, and if you define a character pointer, be sure to initialize it.

Example

Char s[100]; OK.

Char *s; No, because just a pointer is declared. However, it does not allocate a memory buffer for it.

So, if you want to use a pointer, char *s= (char *) malloc (100*sizeof (char));      Allocate memory space for it, C + + with char *s=new char [100]; If the memory space is allocated, the problem is not checked at compile time, but an unknown error occurs at run time:

Fgets (...) Two situations when reading a text line.

1. If n is greater than the string length of a line, fgets (..) when the newline character at the end of the string is read. will return. And at the end of s the insertion string ends with the sign ' \ s '. The remaining positions of the S buffer are no longer populated.

Example

123abc

Fgets (S,10,FP);

At this point, read in seven characters, 123abc\n, and actually have the last ' \ ', so, strlen (s) = 7; If you want to remove the end of \n,s[strlen (s) -1]= ' ";

2. If n is less than or equal to the length of a string, then the n-1 character is read, and is not read at this time because it is not at the end of the line, and it will be inserted at the end.

Example

123abc

Char s[5];

Fgets (S,5,FP);

At this point read 4 characters, 123a, and no newline character, so strlen (s) =4.

Fgets (...) Read the entire file contents

Usually a while () loop is used to make fges () read the entire contents of the text and read in by line.

Char s[1024];

while ((Fgets (S,1024,FP))!=null)

{

printf (s);

}

Of course, if n is less than the number of characters per line, it can be read, but read more often.

Suppose a behavior: 123456789

Char s[2];

int num=0;

while ((Fgets (S,2,FP))!=null)

{

printf (s);

n++;

}

Every time a character is read, the end will read a line, num=10, read 10 times, so, fgets if you do not encounter a newline character, will continue to read in the previous position n-1 characters, as long as the text stream is not closed.

When a blank line is read:

First line abcdef123

Second line

Line three HelloWorld

Where the second act is empty, Fget (..) Will read the second line as well, because it is not at the end of the file.

Sometimes we don't need a blank line, so we can do that.

while ((Fgets (S,N,FP))!=null)

{

if (strlen (s)!=1)//Note here is 1 not 0, because although it is a blank line, it will also read into the newline character, strlen (s) = 1;

printf (s);

}

Fgets (...) Read data from standard devices.

With Fgets (...) Also read into the standard input device (typically the keyboard) information

Prototype: Fgets (S,n,stdin);

Suppose under the console, we can use Fgets (...) Instead of get (), read in the keyboard input information, fgets () is safe, because there is no possibility of overflow like gets ().

For example: Enter ABC

Fgets (S,n,stdin) also reads in n-1 characters. But just from the stdin stream read into ...

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

For ANSI C programs, the runtime system will open at least three streams, 3 of which include:

1. Standard input. The standard is defined as stdin.

2 standard output. The standard is defined as stdout

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

Also, 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 used as a character array, as well as a character pointer, or as a parameter directly using a string constant.

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 possible, in essence, the direct value of a string in C is actually a pointer.

return value:

If the write succeeds, it returns a non-0, at which point the compiler defaults back to 1.

If the error is written, EOF is returned.

Note: fputs (char *s, file *stream) function writes data to a file successfully, and the position pointer is automatically moved backwards.

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

Since the file structure can point to three streams, it can also point to the stdout stream

So:

Fputs ("Hello World", stdout);

Just want the screen output Hello Word.

Finally take a look at fputs (...) Standard library implementations of functions:

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

}

C Language File Operation Fgets ()/fputs ()

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.