Home > Back-end >  About the gets () function return values
About the gets () function return values

Time:02-19

Excuse me exactly how to input, ability makes gets return NULL?
As legend: the if (ret_val)

CodePudding user response:

Windows


Linux

CodePudding user response:

The fgets () returns s on success, and NULL on error or when the end of the file occurs while no characters have had been read.

Input EOF can return NULL, EOF the way, please see the above reply!

CodePudding user response:

Reference:
//baidu encyclopedia explanation for the fgets function 
//fgtes function prototype is: char * the fgets (char * buf, int bufsize, FILE * stream).
//from the file structure pointer data read from the stream, each reading a line, read data stored in buf points to an array of characters,
//most read bufsize - a character at a time (the first bufsize assignment of character '\ 0'), if the file is in the bank, insufficient bufsize characters,
//read it ends, if the bank (including the final newline), the number of characters than bufsize - 1 is the fgets only returns a
//incomplete line, but always ends in NULL character buffer, the next call will continue to read the bank of the fgets function returns buf success,
//fail or read documents ending returns NULL, so we can't directly through the fgets to judge the function return value is wrong and termination, should
//use feof function or ferror function to judge,

Reference://https://my.oschina.net/u/3979769/blog/2208662
//this usage, below is the judgment of the safety file read error or a good way, don't use while (! The feof (fp)),
//and for the second parameter is the biggest the fgets can read the file number of the characters, generally the largest length is 1024 bytes,
//while the fgets (... , the stream)) {

/*... */
//}

//if ferror (stream)) {

/*... */
//}

//the stream: file pointer, if read keyboard input string, fixed writing for stdin
//(1) if the keyboard input number of characters less than bufsize - 1, the function will return record, print output is to enter;
//(2) if the keyboard input of characters is greater than the bufsize - 1, a function go bufsize - 1 characters recorded, the rest of the characters in the data buffer, the next
//read buffer content directly, will not skip input, such as keyboard input, cause the failure of the input,

//the return value
//1. Successful, it returns the first parameter buf.
//2. When reading characters eof, eof indicator is set, if you haven't read any character encountered this kind of situation, buf to keep the original content, returns NULL.
//3. If there is a reading error, the error indicator is set, returns NULL, buf value may be changed,

//how to handle the transfer line operator?
//traversal string, meet a newline, characters and replace it into empty, empty characters, discarding the input line is the rest of the characters,

//why do you want to throw away for too long the remaining characters in the input line?
//the extra character in the input line will be in the buffer, become the input of the next read statements,

//read the entire line input and empty instead of a newline characters, or read some input, and discard the rest
Char * s_gets (char * st, int n)
{
Char * ret_val;
Int I=0;
Ret_val=the fgets (st, n, stdin);
If (ret_val!=NULL)
{
While (st [I]!='\ n' & amp; & St [I]!='\ 0')
i++;

If (st [I]=='\ n')
St [I]='\ 0';
The else
While (getchar ()! )
='\ n'continue;
}
Return ret_val;
}
  • Related