Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use c code

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to use c code". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use c code!

Note that because this code is very general, it may require some other code and small changes for it to actually run on your machine. These changes are necessary because there are different approaches to input and output to the user between Active Server Pages and Windows Scripting Host.

To run the code on Active Server Pages, take the following steps:

Create a standard Web page with the suffix .asp.

Copy the following sample code to & ltBODY >. In the file between the tags.

Put all the code wrappers into the tag.

Move the Option Explicit statement from its current location to the top of the HTML page, even before starting the tag.

Place the tag around the Option Explicit statement to ensure that it runs on the server side.

Add the following code to the end of the sample code:

Sub Print (x)

Response.Write "& ltFONT FACE="Song style", "SIZE="1">"

Response.Write x

Response.Write ""

End Sub

Main

The previous code adds a printing process that will run on the server side but display the results on the client side. To run this code on Windows Scripting Host, add the following code to the end of the sample code:

Sub Print (x)

WScript.Echo x

End Sub

Main

Here is the sample code:

''

'FileSystemObject sample code

'Copyright 1998 Microsoft Corporation. All rights reserved.

''

'' OptionExplicit'''

'To

'for code quality:

The following code has a lot of string operations, concatenating short strings together with the "&" operator. Due to

String concatenation is time-consuming, so this is an inefficient way to write code. In any case, it is

A very maintainable way of writing code, and this method is used here because the program executes

'A lot of disk operations, which are much slower than the memory operations required to connect strings.

Remember that this is a demonstration code, not a product code.

'

'2) "Option Explicit" is used because accessing declared variables is better than accessing undeclared variables

A little faster. It can also prevent errors in the code, such as misspelling DriveTypeCDROM

It became DriveTypeCDORM.

'

'3) to make the code more readable, there is no error handling in the code. Although precautions have been taken to ensure that the code

There are no errors under normal circumstances, but the file system is unpredictable. In the product code, use the

'On Error Resume Next and Err objects to catch errors that may occur.

''

''

'some global variables that are easy to get

''

Dim TabStop

Dim NewLineConst TestDrive = "C"

Const TestFilePath = "C:\ Test"''

''

'constant returned by Drive.DriveType

''

Const DriveTypeRemovable = 1

Const DriveTypeFixed = 2

Const DriveTypeNetwork = 3

Const DriveTypeCDROM = 4

Const DriveTypeRAMDisk = 5 million dollars'

''

'constant returned by File.Attributes

''

Const FileAttrNormal = 0

Const FileAttrReadOnly = 1

Const FileAttrHidden = 2

Const FileAttrSystem = 4

Const FileAttrVolume = 8

Const FileAttrDirectory = 16

Const FileAttrArchive = 32

Const FileAttrAlias = 64

Const FileAttrCompressed = 128 million'

''

'the constant used to open the file

''

Const OpenFileForReading = 1

Const OpenFileForWriting = 2

Const OpenFileForAppending = 8''

''

'ShowDriveType

'Objective:

'generate a string describing the drive type of the given Drive object.

'demonstrate the following

'- Drive.DriveType

''

Function ShowDriveType (Drive) Dim S

Select Case Drive.DriveType

Case DriveTypeRemovable

S = "Removable"

Case DriveTypeFixed

S = "Fixed"

Case DriveTypeNetwork

S = "Network"

Case DriveTypeCDROM

S = "CD-ROM"

Case DriveTypeRAMDisk

S = "RAM Disk"

Case Else

S = "Unknown"

End Select ShowDriveType = SEnd Function'''

'To

'ShowFileAttr

'Objective:

'generate a string to describe the properties of the file or folder.

'demonstrate the following

'- File.Attributes

'- Folder.Attributes

''

'' Function ShowFileAttr (File) 'File can be a file or folder Dim S

Dim Attr

Attr = File.Attributes If Attr = 0 Then

ShowFileAttr = "Normal"

Exit Function

End If If Attr And FileAttrDirectory Then S = S & "Directory"

If Attr And FileAttrReadOnly Then S = S & "Read-Only"

If Attr And FileAttrHidden Then S = S & "Hidden"

If Attr And FileAttrSystem Then S = S & "System"

If Attr And FileAttrVolume Then S = S & "Volume"

If Attr And FileAttrArchive Then S = S & "Archive"

If Attr And FileAttrAlias Then S = S & "Alias"

If Attr And FileAttrCompressed Then S = S & "Compressed" ShowFileAttr = SEnd Function'''

'To

'GenerateDriveInformation

'Objective:

'generate a string that describes the current state of the available drive.

'demonstrate the following

'- FileSystemObject.Drives

'- Iterating the Drives collection

'- Drives.Count

'- Drive.AvailableSpace

'- Drive.DriveLetter

'- Drive.DriveType

'- Drive.FileSystem

'- Drive.FreeSpace

'- Drive.IsReady

'- Drive.Path

'- Drive.SerialNumber

'- Drive.ShareName

'- Drive.TotalSize

'- Drive.VolumeName

''

'' Function GenerateDriveInformation (FSO) Dim Drives

Dim Drive

Dim S Set Drives = FSO.Drives

S = "Number of drives:" & TabStop & Drives.Count & NewLine & NewLine 'construct the first line of the report.

S = S & String (2, TabStop) & "Drive"

S = S & String (3, TabStop) & "File"

S = S & TabStop & "Total"

S = S & TabStop & "Free"

S = S & TabStop & "Available"

S = S & TabStop & "Serial" & NewLine 'construct the second line of the report.

S = S & "Letter"

S = S & TabStop & "Path"

S = S & TabStop & "Type"

S = S & TabStop & "Ready?"

S = S & TabStop & "Name"

S = S & TabStop & "System"

S = S & TabStop & "Space"

S = S & TabStop & "Space"

S = S & TabStop & "Space"

S = S & TabStop & "Number" & NewLine 'delimited lines.

S = S & String (105, "-") & NewLine For Each Drive In Drives

S = S & Drive.DriveLetter

S = S & TabStop & Drive.Path

S = S & TabStop & ShowDriveType (Drive)

S = S & TabStop & Drive.IsReady If Drive.IsReady Then

If DriveTypeNetwork = Drive.DriveType Then

S = S & TabStop & Drive.ShareName

Else

S = S & TabStop & Drive.VolumeName

End If

S = S & TabStop & Drive.FileSystem

S = S & TabStop & Drive.TotalSize

S = S & TabStop & Drive.FreeSpace

S = S & TabStop & Drive.AvailableSpace

S = S & TabStop & Hex (Drive.SerialNumber)

End If S = S & NewLine Next GenerateDriveInformation = SEnd Function'''

'To

'GenerateFileInformation

'Objective:

'generate a string to describe the current state of the file.

'demonstrate the following

'- File.Path

'- File.Name

'- File.Type

'- File.DateCreated

'- File.DateLastAccessed

'- File.DateLastModified

'- File.Size

''

'Function GenerateFileInformation (File) Dim S S = NewLine & "Path:" & TabStop & File.Path

S = S & NewLine & "Name:" & TabStop & File.Name

S = S & NewLine & "Type:" & TabStop & File.Type

S = S & NewLine & "Attribs:" & TabStop & ShowFileAttr (File)

S = S & NewLine & "Created:" & TabStop & File.DateCreated

S = S & NewLine & "Accessed:" & TabStop & File.DateLastAccessed

S = S & NewLine & "Modified:" & TabStop & File.DateLastModified

S = S & NewLine & "Size" & TabStop & File.Size & NewLine GenerateFileInformation = SEnd Function''''

'GenerateFolderInformation

'Objective:

'generate a string to describe the current state of the folder.

'demonstrate the following

'- Folder.Path

'- Folder.Name

'- Folder.DateCreated

'- Folder.DateLastAccessed

'- Folder.DateLastModified

'- Folder.Size

'' Function GenerateFolderInformation (Folder) Dim S S = "Path:" & TabStop & Folder.Path

S = S & NewLine & "Name:" & TabStop & Folder.Name

S = S & NewLine & "Attribs:" & TabStop & ShowFileAttr (Folder)

S = S & NewLine & "Created:" & TabStop & Folder.DateCreated

S = S & NewLine & "Accessed:" & TabStop & Folder.DateLastAccessed

S = S & NewLine & "Modified:" & TabStop & Folder.DateLastModified

S = S & NewLine & "Size:" & TabStop & Folder.Size & NewLine GenerateFolderInformation = SEnd Function''''

'GenerateAllFolderInformation

'Objective:

'generate a string that describes the current state of a folder and all files and subfolders.

'demonstrate the following

'- Folder.Path

'- Folder.SubFolders

'- Folders.Count

Function GenerateAllFolderInformation (Folder) Dim S

Dim SubFolders

Dim SubFolder

Dim Files

Dim File S = "Folder:" & TabStop & Folder.Path & NewLine & NewLine

Set Files = Folder.Files If 1 = Files.Count Then

S = S & "There is 1 file" & NewLine

Else

S = S & "There are" & Files.Count & "files" & NewLine

End If If Files.Count 0 Then

For Each File In Files

S = S & GenerateFileInformation (File)

Next

End If Set SubFolders = Folder.SubFolders If 1 = SubFolders.Count Then

S = S & NewLine & "There is 1 sub folder" & NewLine & NewLine

Else

S = S & NewLine & "There are" & SubFolders.Count & "sub folders" & NewLine & NewLine

End If If SubFolders.Count 0 Then

For Each SubFolder In SubFolders

S = S & GenerateFolderInformation (SubFolder)

Next

S = S & NewLine

For Each SubFolder In SubFolders

S = S & GenerateAllFolderInformation (SubFolder)

Next

End If GenerateAllFolderInformation = SEnd Function'''

'To

'GenerateTestInformation

'Objective:

'generate a string describing the current state of the C:\ Test folder and all files and subfolders.

'demonstrate the following

'- FileSystemObject.DriveExists

'- FileSystemObject.FolderExists

'- FileSystemObject.GetFolder

''

'' Function GenerateTestInformation (FSO) Dim TestFolder

Dim S If Not FSO.DriveExists (TestDrive) Then Exit Function

If Not FSO.FolderExists (TestFilePath) Then Exit Function Set TestFolder = FSO.GetFolder (TestFilePath) GenerateTestInformation = GenerateAllFolderInformation (TestFolder) End Function'

'To

'DeleteTestDirectory

'Objective:

'Clean the test directory.

'demonstrate the following

'- FileSystemObject.GetFolder

'- FileSystemObject.DeleteFile

'- FileSystemObject.DeleteFolder

'- Folder.Delete

'- File.Delete

''

'' Sub DeleteTestDirectory (FSO) Dim TestFolder

Dim SubFolder

Dim File

'There are two ways to delete a file: FSO.DeleteFile (TestFilePath & "\ Beatles\ OctopusGarden.txt") Set File = FSO.GetFile (TestFilePath & "\ Beatles\ BathroomWindow.txt")

File.Delete 'there are two ways to delete a folder:

FSO.DeleteFolder (TestFilePath & "\ Beatles")

FSO.DeleteFile (TestFilePath & "\ ReadMe.txt")

Set TestFolder = FSO.GetFolder (TestFilePath)

TestFolder.DeleteEnd Sub''

''

'CreateLyrics

'Objective:

'create two text files in the folder.

'demonstrate the following

'- FileSystemObject.CreateTextFile

'- TextStream.WriteLine

'- TextStream.Write

'- TextStream.WriteBlankLines

'- TextStream.Close

''

'' Sub CreateLyrics (Folder) Dim TextStream

Set TextStream = Folder.CreateTextFile ("OctopusGarden.txt")

TextStream.Write ("Octopus' Garden") 'Please note that this statement does not add line breaks to the file.

TextStream.WriteLine ("(by Ringo Starr)")

TextStream.WriteBlankLines (1)

TextStream.WriteLine ("I'd like to be under the sea in an octopus' garden in the shade,")

TextStream.WriteLine ("He'd let us in, knows where we've been-- in his octopus' garden in the shade.")

TextStream.WriteBlankLines (2)

TextStream.Close Set TextStream = Folder.CreateTextFile ("BathroomWindow.txt")

TextStream.WriteLine ("She Came In Through The Bathroom Window (by Lennon/McCartney)")

TextStream.WriteLine ("")

TextStream.WriteLine ("She came in through the bathroom window protected by a silver spoon")

TextStream.WriteLine ("But now she sucks her thumb and wanders by the banks of her own lagoon")

TextStream.WriteBlankLines (2)

TextStream.CloseEnd Sub''''

'GetLyrics

'Objective:

'display the contents of the lyrics file.

'demonstrate the following

'- FileSystemObject.OpenTextFile

'- FileSystemObject.GetFile

'- TextStream.ReadAll

'- TextStream.Close

'- File.OpenAsTextStream

'- TextStream.AtEndOfStream

'- TextStream.ReadLine

''

'Function GetLyrics (FSO) Dim TextStream

Dim S

Dim File 'there are several ways to open a text file, and several ways to read data from a file.

'There are two ways to open and read a file: Set TextStream = FSO.OpenTextFile (TestFilePath & "\ Beatles\ OctopusGarden.txt", OpenFileForReading)

S = TextStream.ReadAll & NewLine & NewLine

TextStream.Close Set File = FSO.GetFile (TestFilePath & "\ Beatles\ BathroomWindow.txt")

Set TextStream = File.OpenAsTextStream (OpenFileForReading)

Do While Not TextStream.AtEndOfStream

S = S & TextStream.ReadLine & NewLine

Loop

TextStream.Close GetLyrics = S

End Function'

'To

'BuildTestDirectory

'Objective:

'create a directory hierarchy to demonstrate FileSystemObject.

'create a hierarchical structure in this order:

'C:\ Test

'C:\ Test\ ReadMe.txt

'C:\ Test\ Beatles

'C:\ Test\ Beatles\ OctopusGarden.txt

'C:\ Test\ Beatles\ BathroomWindow.txt

'demonstrate the following

'- FileSystemObject.DriveExists

'- FileSystemObject.FolderExists

'- FileSystemObject.CreateFolder

'- FileSystemObject.CreateTextFile

'- Folders.Add

'- Folder.CreateTextFile

'- TextStream.WriteLine

'- TextStream.Close

'To

'Function BuildTestDirectory (FSO) Dim TestFolder

Dim SubFolders

Dim SubFolder

Dim TextStream

'exclude situations where (a) the drive does not exist, or (b) the directory to be created already exists. If Not FSO.DriveExists (TestDrive) Then

BuildTestDirectory = False

Exit Function

End If If FSO.FolderExists (TestFilePath) Then

BuildTestDirectory = False

Exit Function

End If Set TestFolder = FSO.CreateFolder (TestFilePath) Set TextStream = FSO.CreateTextFile (TestFilePath & "\ ReadMe.txt")

TextStream.WriteLine ("My song lyrics collection")

TextStream.Close Set SubFolders = TestFolder.SubFolders

Set SubFolder = SubFolders.Add ("Beatles")

CreateLyrics SubFolder

BuildTestDirectory = TrueEnd Function''''

'To

'main program

'first, it creates a test directory, as well as some subfolders and files.

'then it dumps some information about available disk drives and test directories

'finally, clear the test directory and all its contents.

'To

'Sub Main Dim FSO' sets the global variable.

TabStop = Chr (9)

NewLine = Chr (10)

Set FSO = CreateObject ("Scripting.FileSystemObject") If Not BuildTestDirectory (FSO) Then

Print "Test directory already exists or cannot be created. Cannot continue."

Exit Sub

End If Print GenerateDriveInformation (FSO) & NewLine & NewLine

Print GenerateTestInformation (FSO) & NewLine & NewLine

Print GetLyrics (FSO) & NewLine & NewLine

DeleteTestDirectory (FSO) End Sub

At this point, I believe you have a deeper understanding of "how to use c code". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 217

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report