C language library function call examples

Source: Internet
Author: User
C language library function calls several examples-general Linux technology-Linux programming and kernel information, the following is a detailed reading. In the C language programming practice, I found that although the compiled program is syntactic
It is correct and can be debugged, but the actual execution results cannot achieve the purpose of programming.
A few examples are as follows, hoping to help beginners in C language with less detours?
1. Cause and solution of unlink call failure
The unlink prototype is in "io. h" and the call method is unlink (filename)
The function is to delete the file specified by filename. The call method is shown in Example 1?
Example 1. delete files in a directory that meet the specified conditions
1 # include
2 void main (int argc, char * argv [])
3 {int done;
4 struct ffblk f;
5 if (argc! = 2) exit (0 );
6 done = findfirst (argv [1], & f, 0 );
7 if (! Done)
8 {if (f. ff_attrib! = 0x10)
9 {unlink (f. ff_name );
10 while (! Findnext (& f ))
11 {if (f. ff_attrib! = 0x10)
12 {unlink (f. ff_name );
13 }}}
14 else while (! Find? Next (& f ))
15 {if (f. ff_attrib! = 0x10)
16 {unlink (f. ff_name );
17 }}}}
This program is syntactically correct and can debug and generate executable files,
However, only files in the current directory that meet the specified conditions can be deleted.
File because the directory path provided by the command line fails to be passed to unlink.
To obtain the directory path provided by the command line.
: Fnsplit (argv [1], drive, dir, name, ext); str? Cpy (pat
H, drive); strcat (path, dir );
Use one of the following two methods to ensure that the directory path specified by the command line is
Call unlink:
1. Insert the "chdir (path);" statement before the statement of Row 3.
2. Use "strcpy (path1, path); strcat (path1, f. ff_name
); Unlink (path1); "replace" unlink (f. ff_name); "in Example 1 );"
Statement.
In addition, because the remove function is implemented by calling the macro unlink
The remove function can be successfully called only after the preceding operations are performed before the move function.
Ii. Correct Method for calling memcpy Functions
The general call method of the memcpy function is "memcpy (dest, src, n );".
The function is to copy the first n characters in the source string src to the destination string dest. Therefore, the destination string de
The last length of st should be n. When the destination string is null or the original length of the destination string is not greater than n,
The memcpy result is correct, and when the original length of the target string is greater than n, memcp is called.
The result obtained after the y function is incorrect, as shown in example 2.
Example 2. Compile the memcpy function demo program
# Include
# Include
# Include
Void main (void)
{Int I;
Static char dest [7] = "First ";
Static char src [] = "Second ";
Printf ("\ nTarget string 1 is: % s", dest );
Memcpy (dest, src, 3 );
Printf ("\ nTarget string 2 is: % s", dest );
Memcpy (dest, src, strlen (src ));
Printf ("\ nTarget string 3 is: % s", dest );
}
The output result of this program should be: Target string 1 is: First
Target string 2 is: Sec
Target string 3 is: Second
However, the actual output result is: Target string 1 is: First.
Target string 2 is: Secst
Target string 3 is: Second
Obviously, calling the memcpy function in this way may result in incorrect results.
The statement replaces "memcpy (dest, src, n);" to get the correct result.
If (strlen (dest)> n)
{For (I = 0; dest ! = '\ 0'; I ++)
Dest= '';/* Assign a space to dest */
Dest= '\ 0 ';
Memcpy (dest, src, n );
}
Else memcpy (dest, src, n );
Of course, you can also compile a memcpy function to replace the database function memcpy.
The memcpy function compiled by the author is not provided. It has the same problems as the memcpy function.
Is there mem for the function? The move function can obtain the correct result after processing the preceding method.
3. correct methods for calling findfirst and findnext Functions
To search, display, or delete files or subdirectories in a program
In general, it is implemented by using the findfirst and findnext library functions. From the perspective of most such applications published in magazines and newspapers (such as software reports), most of them are implemented using the example 3 11th-
The call method shown in row 19.
Example 3. The programming requirements are the same as those in Example 1.
1 # include
2 void main (int argc, char * argv [])
3 {char path [MAXPATH], path1 [MAXPATH];
4 char drive [MAXDRIVE], dir [MAXDIR], name [MAXFILE]
, Ext [MAXEXT];
5 int done;
6 struct ffblk f;
7 if (argc! = 2) exit (0 );
8 fnsplit (argv [1], drive, dir, name, ext );
9 strcpy (path, drive );
10 strcat (path, dir );
11 done = findfirst (argv [1], & f, 0 );
12 while (! Done)
13 {if (f. ff_attrib! = 0x10)
14 {strcpy (path1, path );
15 strcat (path1, f. ff_name );
16 unlink (path1 );
17 done = findnext (& f );
18}
19 done = findnext (& f );
20 }}
This program aims to delete the files in the specified directory, but the actual execution result is consistent
Half of all files under the condition are deleted. You need to run the program multiple times to delete all files.
Delete. You can use the "... if (! Done)... wh
Ile (! Findnext (& f)... "loop statement to solve this problem.
The above programs are all debugged in 5X86/133, Borland C ++ 2.0, and Turbo C.
Pass.
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.