3D PLM Enterprise Architecture |
Middleware Abstraction |
Managing Directories and FilesCreating, opening, and closing directories and files, and managing access permissions |
Use Case |
AbstractThis article shows how to manage directories and files. |
This use case is intended to show you how to manage directories and files.
[Top]
CAASysFileOperations is a use cases of the CAASystem.edu framework that illustrates System framework capabilities.
[Top]
This use case creates a directory in a directory passed as input, and creates ten files in this created directory. It checks the directory access permissions beforehand, and if the directory to create doesn't exist. It then scan the created directory and makes a list of the created files. It should be launched as follows from the command line:
CAASysFileOperations WorkingDir NewDir
where WorkingDir
is the directory in which NewDir
should be created.
[Top]
To launch CAASysFileOperations, you will need to set up the build time environment, then compile CAASysFileOperations along with its prerequisites, set up the run time environment, and then execute the use case [1].
[Top]
The CAASysFileOperations use case is made of a several classes located in the CAASysFileOperations.m module of the CAASystem.edu framework:
Windows | InstallRootDirectory\CAASystem.edu\CAASysFileOperations.m\ |
Unix | InstallRootDirectory/CAASystem.edu/CAASysFileOperations.m/ |
where InstallRootDirectory
is the directory where the CAA CD-ROM
is installed.
[Top]
To create a directory, create files in it, scan the created directory, and close the directory, there are six main steps:
# |
Step |
---|---|
1 | Check the access permissions |
2 | Create a directory path |
3 | Check that the directory path doesn't already exists |
4 | Create the directory and the files |
5 | Scan the created directory |
6 | Close the directory |
[Top]
The two command line parameters are retrieved in WrkDir
and DirName
respectively.
... CATLibStatus status = ::CATFileAccess(WrkDir, W_OK); if (CATLibError == status) { exit(-1); } ... |
The access permissions to the WrkDir
directory in which a new
one should be created are checked using the CATFileAccess
global
function, with the parameter W_OK
, that makes CATFileAccess
checks for writing permission. If this permission is not available, the status
returned is CATLibError
, and the program ends.
[Top]
The access permissions enables the creation of a directory in WrkDir
.
... char Path[1024]; ::CATMakePath(WrkDir, DirName, Path); ... |
The CATMakePath
global function creates a path name for the
directory DirName
in WrkDir
. This pathname is returned
in Path
.
[Top]
... status = ::CATFileAccess(Path, F_OK); if (CATLibSuccess == status) { cout << " A file or directory named " << DirName << " already exists" <<endl; CATFileInfo Stat; status = ::CATGetFileInfo(Path, &Stat); if (CATLibError == status) { cout << " Can't access to file information" << endl; exit (-1); } if (S_IFDIR != (Stat.st_mode & S_IFDIR)) { cout << Path << " is a file" << endl; exit (-1); } else cout << "Directory " << Path << " already exists " << endl; } ... |
The CATFileAccess
global function checks that a file with the
pathname Path
doesn't already exist. F_OK
requests
file (or directory) existence checking. If the file exists, the CATGetFileInfo
global function retrieves the file information in the Stat
structure. Then, Stat
is used to determine whether Path
refers to a file or a directory. If a file exists, nothing else can be done, and
the programs stops, but if this is a directory, it will be used.
[Top]
The access permissions enables the creation of a directory in WrkDir
.
... else { status = ::CATCreateDirectory(Path); if (CATLibError == status) { cout << " Can't create " << Path <<endl; exit (-1); } for (int i=0; i< 10; i++) { char name[14]; sprintf(name, "FILE%x", i); char FilePath[1024]; ::CATMakePath(Path, name, FilePath); FILE *fd = fopen(FilePath, "w+b"); fclose(fd); } ... |
The CATCreateDirectory
global function creates a directory with
the path name Path
. Then the CATMakePath
global
function creates in the Path
directory as many files as requested
with the names FILE0
, FILE1
, and so on up to FILE9
.
w+b
means that these files are in write mode, and have the binary
format.
[Top]
The files created are now scanned, and their file names displayed.
... CATDirectory Dir; status = ::CATOpenDirectory(Path, &Dir); if (CATLibError == status) { cout << " Can't open directory " << Path << endl; exit (-1); } int EndOfDir=0; CATDirectoryEntry Entry; cout << "Listing of the Directory " << Path << endl; while ((EndOfDir != 1) && (CATLibSuccess == status)) { status = ::CATReadDirectory(&Dir, &Entry, &EndOfDir); if ((CATLibError == status) && (EndOfDir !=1)) { cout << " Can't read next entry in " << Path << endl; exit (-1); } cout << Entry.name << endl; } ... |
The CATOpenDirectory
global function opens the created
directory, and if this opening is successful, the directory is scanned using the
CATReadDirectory
global function that returns the file name in Entry
,
and whether the end of the directory is reached by returning 1 or 0 in EndOfDir
,
1 meaning that the end is reached.
[Top]
The directory can now be closed.
... status = ::CATCloseDirectory(&Dir); if (CATLibError == status) { cout << " Can't close the directory " << Path << endl; exit (-1); } ... |
The CATCloseDirectory
global function closes the directory.
[Top]
This use case shows how to create a directory, create files in it, scan the created directory, and close the directory.
[Top]
[1] | Building and Launching a CAA V5 Use Case |
[Top] |
Version: 1 [Mar 2000] | Document created |
[Top] |
Copyright © 2000, Dassault Systèmes. All rights reserved.