Professor Zhou Ligong's hard work for several years, "Programming and Data Structure", the electronic version has been distributed to electronic engineers and college groups for free, and can be read online after replying [program design] in the public number. After the publication of the book content, there was a learning boom in the electronics industry. Authorized by Professor Zhou Ligong, this public number has serialized the contents of this book, and is willing to share it. The first chapter is the basis of programming. This article is the second point of 1.8.2 string constant: the input and output of the string. (1) scanf () function and gets () function When reading a string, scanf() and the conversion formatter %s can only read one word, such as: Scanf("%s", str); In the scanf function call, you don't need to add & before str, because str is the array name, and the compiler will treat it as a pointer when passing it to the function. When called, the scanf function skips the null character, then reads the character and stores it in str until it encounters a null character. The scanf function always stores a null character at the end of the string. In the program, it is often necessary to read a whole line of input, not just a word, and gets() is used to handle this situation. It reads the entire line of input until it encounters a newline character, then discards the newline character to store the rest of the characters, and adds a null character to the end of the characters to make it a string. It is often paired with puts(), which is used to display strings and add newlines at the end. That is, gets() is to input a number of characters from the standard input device and save them to the character array pointed to by the parameter s until the end of the file or a newline is read. Line breaks will be discarded and a terminator '\0' will be written immediately after the last character is entered. Its function prototype is as follows: Char *gets(char *s); The s point to the memory space where the input string is saved. If gets() successfully obtains the string, it returns s, otherwise it returns NULL. For example, enter a character '9' through the command line, but '9' is not an integer of 9. If you set '9'-'0', you will get an integer of 9. which is: Char cStr[256]; Int cmdNum; cmdNum = getchar() - '0'; Gets(cStr); // Â Empty buffer If the array is passed as a parameter, the pointer to the first element of the array is passed. When gets() is called as the called function, there is no way to know how big the array is, and the caller cannot pass the buffer to gets(). Size, so gets() cannot check the length of the array. Obviously there must be enough space to hold the input string, otherwise there may be some inexplicable problems. If you deliberately pass large data to gets(), you can achieve the purpose of overriding the array and overwriting the return address. In 1988, the "Internet worm" virus of the famous Internet was exploiting the weakness of gets(). Due to the hidden dangers caused by the unsafe behavior of gets(), the committee that formulated the C11 standard adopted a tough attitude and abolished the gets() function directly from the standard. It's a good idea to write an input function yourself, assuming that the function doesn't skip null characters, stops reading at the first newline character (not stored in the string), and ignores extra characters. Its function prototype is as follows: Int readLine(char str[], int n); The readLine() function is mainly composed of a loop. As long as there is space in str, this loop will call the getchar() function to read the characters one by one and store them in str, and terminate the loop when reading the newline. See the program. Listing 1.40. Program Listing 1.40 Implementation of the readLine() function 1 int readLine(char str[], int n) 2 { 3 int ch, i = 0; 4 5 while((ch = getchar()) != '') 6 if(i < n) 7 str[i++] = ch; 8 str[i] = '\0'; 9 return 0; 10 } (2) printf () function and puts () function The conversion format s% allows printf() to write strings. Unlike puts, printf() does not automatically add a newline at the end of each string, so you must specify in the parameter where the line break should be used. symbol. such as: Char str[] = "hello world"; Printf("%s", str); Printf() will write the characters in the string one by one until a null character is encountered. If you only want to display a portion of a string, you can use the conversion format character %.ps, where p is the number of characters displayed. For example, show hello: Printf("%.5s", str); Although printf() is more complicated to use, it can print multiple strings. In addition to printf(), the C standard library also provides puts(), whose function prototype is as follows: Int puts(const char *s); Where s is the string of the specified output, and the puts() function outputs the string pointed to by the parameter s to the standard output device, but does not output the terminator '\0'. After the string is output, the puts() function will output a newline character '' and then display the specified string through the standard output device. Returns 0 if the display is successful, otherwise returns the predefined constant EOF. How does puts() know where to stop? This function stops outputting when it encounters a null character, so you must ensure that there are null characters. (3) fgets () function and fputs () function Fgets() and fputs() are the customized versions of gets() and puts() for files. fgets() solves the overflow problem by limiting the number of characters read in the second parameter. This function is used to process files. enter. If the value of the second argument is n, then fgets() will read n-1 characters or encounter the first newline. If you read a newline character and store it in a string, unlike gets(), gets() will discard the newline character. The first parameter of fgets() is the same as gets(). It is also the address where the input location is stored (char * type). The second parameter is an integer indicating the size of the string to be input. The last parameter is the file pointer. The file to be read. If the data entered from the keyboard is read, the standard input stdin is used as a parameter, which is defined in stdio.h. An example of its call is as follows: Fgets(buf, STLEN, fp); Among them, buf is the name of the char type array, STLEN is the size of the string, and fp is a pointer to FILE. Using gets() above as an example, fgets() reads the input until the first newline, or reads the end of the file, or reads STLEN-1 characters, then fgets() adds a null character to the end. It becomes a string whose size is the number of characters plus a null character. If fgets() has read an entire line before reading the upper limit of the character, it will put a newline character at the end of the line before the null character. Fegts() will return NULL when it encounters EOF, so you can use this mechanism to check if the end of the file is reached. If no EOF is encountered, its address is returned. Fgets() stores both new and bad points. The downside is that you may not want to store newline characters in strings. Such newline characters can cause some trouble. The advantage is that for stored strings, check if there is a newline at the end to determine if an entire line has been read. If it is not an entire line, the remaining characters in the line should be handled properly. First, how do you handle line breaks? One way is to look for a newline character in the already stored string and replace it with a null character. Assume in st: While(st[i] != '' ) i++; St[i] = '\0'; Second, what if there are still strings left in the input line? One possible solution is to discard the extra characters if the target number is not assembled for the entire line of input. Read but not store input, including: While(getchar() != '\0') Continue; Why discard the remaining characters in the input line? Because the extra characters in the input line will remain in the buffer and become the input to the next read statement. For example, if the next read statement is to read a value of type double, it may cause the program to crash, and discard the remaining characters of the input line to ensure that the read statement is synchronized with the keyboard input. Since there is no such function, create one, and the s_gets() function is shown in Listing 1.41. Listing 1.41 s_gets() function 1 char * s_gets(char *st, int n) 2 { 3 char *ret_value; 4 int i = 0; 5 6 ret_value = fgets(st, n, stdin); 7 if(ret_value){ 8 while(st[i] != '' && st[i] != '\0') 9 i++; 10 if(st[i] == '') 11 st[i] = '\0'; 12 else 13 while(getchar() != '\0') 14 continue; 15 } 16 return ret_value; 17 } If fgets() returns NULL, it means that the end of the file was read or a read error occurred, and s_gets() skipped the process. One of the loops: While(st[i] != '' && st[i] != '\0') i++; Traverse the string until a newline or null character is encountered. If a line break is encountered first, the following if statement replaces it with a null character; if a null character is encountered first, the else part discards the remaining characters of the input line and returns the same value as fgets(). Although s_gets() has been greatly improved for replacing fgets(), it is still not perfect. It does not respond if it encounters an inappropriate input. When it discards extra characters, it does not notify the program or inform the user, please complete the reader. Since fgets() puts a newline character at the end of the string (assuming the input line does not overflow), it is usually paired with fputs() unless the function does not add a newline at the end of the string. The fputs() function accepts two arguments: the first is the address of the string, and the second is the file pointer, indicating the file to be written. The function writes the string found by the incoming address into the specified file. If you want to display it on a computer monitor, you should use the standard output stdout as a parameter. Unlike puts(), puts() does not add a newline at the end of the string when it is printed. An example of its call is as follows: Fputs(buf, fp); Where buf is the address of the string and fp is used to specify the target file. Note that gets() discards newline characters in the input, but puts() adds a newline character to the output. On the other hand, fgets() retains the newline character in the input, and fputs() does not add a newline character to the output.
China In-Ear Wired Earbuds,Earphones With Mic manufacturer, choose the high quality In-Ear Noise-Isolating Earbuds,Wired Earphones With Microphone, etc.
High fidelity popular in -ear and non-Detachable Earphones. suitable wire length with good sound quality,it can be use for computer, mobile phone, portable media player.Easy to carry no mater you are running or working.
In-Ear Wired Earbuds,Earphones With Mic,In-Ear Noise-Isolating Earbuds,Wired Earphones With Microphone Dongguang Vowsound Electronics Co., Ltd. , https://www.vowsound.com