Problems when writing SFSS

Source: Internet
Author: User
Tags function prototype stdin connection reset

1. Control the length of the input string

Fgets (Admin,name_lengh,stdin);

if (Admin[strlen (admin) -1]== ' \ n ')

Admin[strlen (Admin) -1]= ' ";

Fflush (stdin);//the ansi/iso standard specifies that fflush () are useful only on OUTPUT streams

2. scanf does not read into the carriage return, so the carriage return is left in the buffer

Fgets (Tempbuf,sizeof (TEMPBUF), stdin);
while (strcmp (tempbuf, "\ n") ==0)//Discard the carriage return left in the buffer or the user-entered carriage return
Fgets (Tempbuf,sizeof (TEMPBUF), stdin);

3. Clear the input buffer-- if the buffer itself is empty, wait for input

int C;

while ((C=getchar ())!= ' \ n ' && c!=eof);

-------------------------------------------------

Fflush (stdin); Empty the input stream .... Not under Linux.

Fflush (stdout); Empty output stream


--------------------------------------------------------------------------

-Graphical fixed position input and output curses.h

. printf typically has \ n or a buffer full to output to the screen, causing the data not to be displayed on the screen in time, and all can be write (Standout_fileno,str,len) instead of printf

. The backspace displays "^h" on the Screen: System ("Stty erase ^h") can remove characters from ^h, but pressing the backspace always deletes the characters, including not the prompt characters that are not just entered by the user and should not be deleted.

Or use Tcgetattr, tcsetattr function settings to modify keyboard actions, control backspace Delete characters

--------------------------------------------------------

4. No close after writing the file, interrupt Ctrl + C, write the file will not save

5. Comma operation
Suppose B=2,c=7,d=5,
a1= (++b,c--, d+3);
A2=++b,c--, d+3;
For the first line of code, there are three expressions, separated by commas, so the final value should be the value of the last expression, d+3, 8, so a1=8.
For the second line of code, there are also three expressions, at which point the three expressions are a2=++b, C--、 d+3 (this is because the assignment operator is higher precedence than the comma operator) so the value of the final expression is 8, but a2=3. -http://blog.sina.com.cn/s/blog_693d42fc01010ytm.html

In C, a comma (,) can also be an operator, called a comma operator (Comma Operator). The comma operator can concatenate more than two (including two) expressions into an expression called a comma expression. Its general form is:
Child expression 1, subexpression 2, ..., subexpression n
For example:
A + B, C = B, C + +
The precedence of a comma operator is the lowest of all operators, and is usually used in conjunction with a For loop. The value of the rightmost subexpression of a comma expression is the value of a comma expression. In the example above, the value of C + + (the value before the C self-increase) is the value of the expression.
The comma operator guarantees the operation of the right subexpression after the end of the subexpression operation on the left. That is, the comma operator is a sequence point, and all side effects on the left end before the subexpression on the right side of the expression is performed. So, in the example above, C gets the value of B to do the self increment operation.


6. Dynamically allocating Memory: http://blog.csdn.net/firecityplans/article/details/4490124

void *malloc (unsigned size)//Dynamic request size byte of memory space; function : Allocate a contiguous region of length "size" in the dynamic storage area of memory. The return value of the function is the first address of the range ... (type specifier *) indicates that the return value is cast to the type pointer.

(void *) calloc (unsigned n,unsigned size)//is used to dynamically request n for the system, each memory space that occupies the size byte; And the allocated memory is initialized to 0 values. The return value of the function is the first address of the range

(void *) realloc (void *p,unsigned size) Change the allocated memory area pointed to by the pointer p to size

difference: Both are dynamically allocated memory. The main difference is that malloc does not initialize the allocated memory, which can be any value in the allocated memory. Calloc Initializes an allocated memory of 0.


Memory copy function prototype: void *memcpy (void *dest, const void *SRC, size_t n);
function: Copy n bytes from the starting position of the memory address indicated by source SRC to the starting position of the memory address referred to by Target dest


6.socket problem: http://blog.csdn.net/rstevens/article/details/3284661

A. Connection Reset by peer
If you call read () to receive data from a TCP connection and return-1, and errno is Connection reset by peer, this usually means that the End-to-end program exits directly without closing the socket (for example, Core dump);
A normal call to close () to shut down a socket causes the two handshake of the connection to be turned off, which requires a little network interaction time, in which case read () returns 0.
If the end-program does not explicitly call Close () to turn off a TCP connection, the operating system releases the associated resources before the process exits, including closing the open file descriptor, but in this case the shutdown is done immediately, so read () returns-1 and Err No is 104.
Therefore, in the development process, encountered this phenomenon, you can determine the end of the program is not a problem.

When thread a keeps the socket with a close () in read (), it returns 0.

When you start a thread B, a pair of new sockets communicate with thread A, especially when sending data to each other, write may return-1, errno (Connection reset by peer). It is possible that thread A has been caused by a read () of a close () socket (not a new socket created between thread A and B), as long as the read is stopped in thread a-------don't know why.
B. Write () call returns-1 and errno is Eagain
This behavior may occur when you increase the use of write () in a non-blocking socket.
On a non-blocking socket, the write () operation simply returns the data into the kernel space, and this behavior occurs if the socket has a full buffer in the kernel space.

There are two ways to handle this:
The first is simply to resend the data at a later time
The second is through the Selecet (), poll () and other system calls, when the socket can write notification, then send the data


C. Communication with a disconnected socket, the bottom will throw a sigpipe signal (the other process exits, you write once to him, you will receive the RST paragraph, and then write to him, you will receive the sigpipe signal; the other process exits without close, He will turn off all file descriptions, that is, the system closes the socket and sends a RESET package to you, and you read () returns-1 and errno for 104-connection RESET by peer. Can be used to determine if the other person is online

Signal (Sigpipe,receivepipbroken);

void Receivepipbroken (int sign_no)
{
if (sign_no = = sigpipe)
{
printf ("A client exit!\n");
Currentclientnum--;
Pthread_exit (0);
}
}



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.