Use C language applets to solve big problems

Source: Internet
Author: User
Use C language applets to solve major problems-general Linux technology-Linux programming and kernel information. The following is a detailed description. We know that C language has very powerful functions and is suitable for compiling system software. Using it, we can easily solve some difficult problems that we often encounter, such as copying hidden or system attribute files (especially the hidden files on the CD) change the subdirectory name in any DOS version or windows, and use a directory name with spaces in any DOS version. These problems are often hard to work with by operating systems or commonly used tool software. We can use the powerful functions of the C language to compile some small programs and solve these problems smoothly.

1. Copy files with implicit or system attributes

The Copy Command provided by the operating system cannot copy files with implicit or system attributes, because the system prompts "File not found" during the copy process ". The following small program can solve this problem.

// Copyfile. c
# Include $ # @ 60; stdio. h $ # @ 62;
# Include $ # @ 60; stdlib. h $ # @ 62;
Main (int argc, char * argv [])
{If (argc $ # @ 60; 3)
{
Printf ("\ n usage: COPYFILE
$ # @ 60; source file name $ # @ 62; $ # @ 60; target file name $ # @ 62; \ n ");
Exit (1 );
}
Copy (argv [1], argv [2]);
}

Copy (char * file1, char * file2)
{
Char ch;
FILE * fp1, * fp2;

If (fp1 = fopen (file1, "rb") = NULL)
{
Printf ("\ nCant open file % s", file1 );
Fclose (fp1 );
Return;
}
If (fp2 = fopen (file2, "wb") = NULL)
{
Printf ("\ nCant open file % s", file2 );
Fclose (fp1 );
Return;
}

Ch = fgetc (fp1 );
While (! Feof (fp1 ))
{
Fputc (ch, fp2 );
Ch = fgetc (fp1 );
}
Fclose (fp1 );
Fclose (fp2 );
Return;
}

2. Change the subdirectory name in any DOS version or windows
DOS only provides programs for changing the sub-register name in version 6.x. Changing the directory name in windows is complicated. The following applet can easily change the name of any level-1 subdirectory, and can also be used to change the name of the file.

# Include "stdio. h"
Main (int argc, char * argv [])
{Int n;
If (argc $ # @ 60; 3)
{
Printf ("Required parameter missing \ n ");
Exit (0 );
}
N = rename (argv [1], argv [2]);
If (n =-1)
{
Printf ("Duplicate name or name not found \ n ");
Exit (0 );
}
}

3. Use the directory name with spaces

The system uses spaces to differentiate the parameters of input DOS commands. Similarly, DOS commands such as MD, CD, and RD cannot use directory names containing spaces. However, when the CHKDSK command is used to check the disk, no error message is provided for the subdirectory name containing spaces. This indicates that the directory names containing spaces in DOS are valid. Because DOS commands cannot use directory names containing spaces, they can be encrypted. The following program describes how to use the C language to create, enter, and delete subdirectories containing spaces.

/* File name: mddir. c */
# Include $ # @ 60; dir. h $ # @ 62;
Main (int argc, char * argv [])
{If (argc = 2)
{If (mkdir (argv [1]) perror ("Error ");}
Else printf ("Usage: MDDIR + dirname \ n ");
Return (0 );
}


Note that you must use double quotation marks to enclose the directory name. If the directory name does not contain any space, double quotation marks can be omitted. For example, mddir "a test ". The following procedures are used in the same way

/* File name: cddir. c */
# Include $ # @ 60; dir. h $ # @ 62;
Main (int argc, char * argv [])
{If (argc = 2)
{If (chdir (argv [1]) perror ("Error ");}
Else printf ("Usage: CDDIR + dirname \ n ");
Return 0;
}


3. Delete subdirectories with spaces

/* File name: rddir. c */
# Include $ # @ 60; dir. h $ # @ 62;
Main (int argc, char * argv [])
{If (argc = 2)
{If (rmdir (argv [1]) perror ("Error ");}
Else printf ("Usage: RDDIR + dirname \ n ");
Return 0;
}

The above programs are all debugged Using Turbo c 2.0. Note that the file names of the above programs should not be the same as the relevant commands in DOS to avoid conflicts.
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.