PC SOFT
DEPOT EN LIGNE
POUR WINDEVWEBDEV ET WINDEV MOBILE

WD Handling text files
Publié par Boller
dans la catégorie Outils
Nouveautés



Description
WD Handling text files

// ----------------------------------------------------------------
// ----------------------- RUN CODE ---------------------------
// ----------------------------------------------------------------

// Identifier of the external file:
// this identifier is used by all the functions for managing the external files
nFileID is int

// Create the "test.txt" file in the Exe directory of the application
nFileID = fCreate(CompleteDir(fExeDir())+"test.txt")

// Check the creation of the file
IF nFileID = -1 THEN
Error("Error while creating the file",ErrorInfo())
RETURN
END

// Close the "test.txt" file
fClose(nFileID)

// Inform the user
Info("Creating the file: "+CompleteDir(fExeDir())+"test.txt"+" was successfully completed")

//-----------------------------------------------------------------------------------------------------

// ----------------------------------------------------------------
// ----------------------- RUN CODE ---------------------------
// ----------------------------------------------------------------

// Identifier of the external file:
// this identifier is used by all the functions for managing the external files
nFileID is int

// Open the "test.txt" file in read/write found in the Exe directory of the application
nFileID = fOpen(CompleteDir(fExeDir())+"test.txt",foReadWrite)

// Check the file opening
IF nFileID = -1 THEN
Error("Error while creating the file",ErrorInfo())
RETURN
END

// Write into "text.txt "
fWriteLine(nFileID,"content of line 1")
IF ErrorOccurred THEN
Error("Error writing to file",ErrorInfo())
RETURN
END

// Write into "text.txt "
fWriteLine(nFileID,"content of line 2")
IF ErrorOccurred THEN
Error("Error writing to file",ErrorInfo())
RETURN
END

// Close the "test.txt" file
fClose(nFileID)

// Inform the user
Info("Writing into the file: "+CompleteDir(fExeDir())

//---------------------------------------------------------------------------------------------

// ----------------------------------------------------------------
// ----------------------- RUN CODE ---------------------------
// ----------------------------------------------------------------

// Write into "text.txt " directly
fSaveText(CompleteDir(fExeDir())+"test.txt","Content of row 1"+CR+"Content of row 2"+CR+"Content of row 3")

// Check the operation
IF ErrorOccurred THEN
Error("An error occurred while writing into the file",ErrorInfo())
RETURN
END

// Inform the user
Info("Writing into the file: "+CompleteDir(fExeDir())+"test.txt"+" was successfully completed")

//------------------------------------------------------------------------------------------------------------------------------

// ----------------------------------------------------------------
// ----------------------- RUN CODE ---------------------------
// ----------------------------------------------------------------


// Identifier of the external file:
// this identifier is used by all the functions for managing the external files
nFileID is int

// Content of the file
sFileContents is string

// Content of the line currently processed
sFileLine is string

// Open the "test.txt" file in read-only found in the Exe directory of the application
nFileID = fOpen(CompleteDir(fExeDir())+"test.txt",foRead)

// Check the file opening
IF nFileID = -1 THEN
Error("Error while opening the file",ErrorInfo())
RETURN
END

// Read the first line of the file
sFileLine = fReadLine(nFileID)

// Check the operation
IF ErrorOccurred THEN
Error("An error occurred while reading the file",ErrorInfo())
RETURN
END

// The file is browsed until the end of file
WHILE NOT sFileLine~=EOT

// Store the line read
IF sFileContents ~="" THEN
sFileContents = sFileLine
ELSE
sFileContents = sFileContents + CR + sFileLine
END

// Read the next row
sFileLine = fReadLine(nFileID)
END

// Inform the user
Info("The file was successfully read, the content of the file is: ",sFileContents)

//-------------------------------------------------------------------------------------------------------------------------------------


// ----------------------------------------------------------------
// ----------------------- RUN CODE ---------------------------
// ----------------------------------------------------------------

// Content of the file
sFileContents is string

// Read the "test.txt" file directly
sFileContents = fLoadText(CompleteDir(fExeDir())+"test.txt")

// Check the operation
IF ErrorOccurred THEN
Error("An error occurred while reading the file",ErrorInfo())
RETURN
END

// Inform the user
Info("The file was successfully read, the content of the file is: ",sFileContents)

//--------------------------------------------------------------------------------------------------------------

Block is string // Block currently read
Rec is string // Record currently checked out
FileName is string = "file1.txt" // Name of the test file
FileNum is int = 0 // identifier used to handle the test file
i is int // Browse subscript

// Prepares the file
fSaveText(FileName, "This is a variable record file;Each record is separated by a semicolon;To read this type of file, we recommend that you use fRead;")

// Open the file in read-only
FileNum = fOpen(FileName,foRead)

// Check the opening of the test file
IF FileNum = -1 THEN
// An error occurred while opening the file
Error(StringBuild("Error opening the %1 file",FileName),ErrorInfo())
RETURN
ELSE
// Read the file by 500-byte blocks
Block = fRead(FileNum,500)
// Read the data as long as data to read is found
IF Length(Block) <> 0 THEN
Rec = ""
i = 1
// Extract the records
LOOP
// Records separated by;
Rec = ExtractString(Block,i,";")
IF Rec = "" THEN BREAK
// Display the checked-out record
Info(Rec)
// Ask to extract the next block
i++
END
END
// Close the file
fClose(FileNum)
END


//------------------------------------------------------------------------------------------------

// Write operations (with lock) in TOTO.TXT
ResLock is boolean // Result of file lock
FileNum is int // identifier used to handle the test file
FileName is string = "TOTO.TXT" // Name of the test file
Str is string // Test string to write into the file

// Initialize the text used for test
Str = CR+"I learn to work with text files"...
+CR+"In lock mode "...
+CR+"It is really simple..."


// Create the file
FileNum = fCreate(FileName)
IF FileNum = -1 THEN
Error(StringBuild("Error creating file %1",FileName))
RETURN
END

Info(StringBuild("The %1 file was successfully created", FileName))
// Open the file in 'Lock' mode
FileNum = fOpen(FileName,foAdd)
IF FileNum <> -1 THEN
// lock the first byte of the file
ResLock = fLock(FileNum,0,1)
IF ResLock = False THEN
Error(StringBuild("Byte 1 of %1 file already locked", FileName))
ELSE
Info("Locked OK")
// Write into the file
fWrite(FileNum,StringBuild("Text written %1 to %2",DateToString(Today()), TimeToString(Now())))
fWrite(FileNum,Str)
IF ErrorOccurred THEN
// An error occurred while writing into the file
Error(StringBuild("Error writing to %1",FileName),ErrorInfo())
RETURN
ELSE
// Close the file
fClose(FileNum)
Info(StringBuild("End of write operation in %1 file",FileName),...
"File Closed")
END
END
ELSE
// An error occurred while opening the file
Error(StringBuild("Error opening %1",FileName),ErrorInfo())
END


//--------------------------------------------------------------------------------------------------

IF fFileExist(fExeDir() + "\test.txt") THEN
IF fCopyFile("test.txt","test2.txt") THEN
Info("The copy of test.txt to test2.txt was performed")
BTN_DeleteFile..State = Active
ELSE
Error("Problem while copying test.txt to test2.txt")
END
ELSE
Error("The test.txt file is not found in the directory",...
fCurrentDir())
END

//-------------------------------------------------------------------------------------------------

// Delete the copied file ("COPY1.TXT") if it exists
IF fDelete("test2.txt") THEN
Info("The deletion of test2.txt was performed")
ELSE
Error("Unable to delete: test2.txt does not exist",...
"Creation by clicking the Copy File button")
END

//-------------------------------------------------------------------------------------------

// Use the file picker of Windows
Res is string = ""
Res = fSelect("",... // Home directory
"",... // Selected file
"",... // Title of the window
"Text file (*.txt)"+TAB+"*.TXT"+CR+... // Choose the extensions
"All (*.*)"+TAB+"*.*",... // Selection Filter
"TXT") // Extension selected from the entry
IF Res <> "" THEN
InfoBuild("The selected file is '%1'",Res)
ELSE
Info("No selected file")
END

//-------------------------------------------------------------------------------------------------------------------

FileName is string = "test.txt"

// Existence of the file
IF fFileExist(FileName) = False THEN
InfoBuild ("The '%1' file does not exist", FileName)
ELSE
InfoBuild("The '%1' file exists", FileName)
END

//------------------------------------------------------------------------------------

FileName is string = "test.txt"

IF fDir(FileName,frFile) <> "" THEN
InfoBuild("Size of '%1': %2" + CR + "Creation Date: %3 "+ CR + "Creation Time: %4" , FileName, LengthToString(fSize(FileName)), DateToString(fDate(FileName)), TimeToString(fTime(FileName)))
ELSE
ErrorBuild("The '%1' file is not found in the current directory", FileName)
END

//--------------------------------------------------------------------------------------------------------

sDisk is string
sDisk = fCurrentDrive()

// Type of disk
SWITCH fDriveInfo(sDisk,fdDriveType)
CASE 0 : ErrorBuild("%1: Drive not ready", sDisk)
CASE 1 : InfoBuild("%1: Disk drive",sDisk)
CASE 2 : InfoBuild("%1: Local hard disk", sDisk)
CASE 4 : InfoBuild("%1: Network disk", sDisk)
CASE 8 : InfoBuild("%1: CD ROM", sDisk)
CASE 16 : InfoBuild("%1: Memory disk", sDisk)
END

//-----------------------------------------------------------------------------------

sRes is string = "" // Name of the selected directory

// Using the Windows directory picker
sRes = fSelectDir("", "Select a directory...", "")

// Was the selection of a directory validated the user
IF sRes <> "" THEN
// A directory was selected by the user
Info(StringBuild("The directory selected is %1", sRes))
ELSE
// No directory was selected by the user
Info("No selected directory")
END


Illustrations, copies d'écran
none
none
Avis des utilisateurs
(Pour noter la ressource, cliquez sur Ecrire un avis)
Aucun avis ou commentaire ? Soyez le premier !