C Language File Operation Fgets ()

Source: Internet
Author: User
Tags stdin

Say a fgets (..) Function.

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

Number of references:

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. If a read-in error or an end-of-file (EOF) is encountered, NULL is returned.

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 reading data, define a character array or a character pointer, assuming that a character pointer is defined, then be sure to initialize it.

Example

Char s[100]; Can.

Char *s; Not able, because only a pointer is declared. However, it does not allocate a memory buffer for it.

So, suppose 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]; Assuming that the memory space is allocated, the problem is not checked at compile time, but an unknown error occurs during execution:

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

1. Assuming that n is greater than the string length of a line, when you read the newline character at the end of the string, Fgets (..) 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; Suppose you want to remove the end of \n,s[strlen (s) -1]= ' n ';

2. Assume that n is less than or equal to the length of a string, then read into the n-1 character, at this time not read in \ n because there is no end to the line, the same at the end will be inserted ' "

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

Pass often uses the while () loop to make fges () read all the 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 also be read, just read more often.

If one behavior: 123456789

Char s[2];

int num=0;

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

{

printf (s);

n++;

}

Read one character at a time, and finally read a line, num=10, read 10 times, so, fgets if not encountered newline character, will continue to read n-1 characters from the previous position, only if the text stream is not closed.

When a blank line is read:

First line abcdef123

Second line

Line three HelloWorld

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 to be free to do this.

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 be read into the line break, 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);

If under the console, we can use Fgets (...) Instead of get (), read in the keyboard input information, fgets () is safe, because it does not have the possibility of overflow like gets ().

For example: Enter ABC

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



C Language File Operation Fgets ()

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.