|
course.wilkes.edu/CS125Labs |
||||||
Lab 0: Getting Started with C++
IntroductionThe main purpose of this lab is to introduce you to the computing environment of your laboratory. You will use the ideas in this lab again and again throughout this course, so you should make every effort to understand not only what, but why you are doing what you are doing at each step. Before we can begin our session, your instructor must inform you how to begin a session with the computer at your particular institution. The way this is done differs from school to school, according to the kind of computer being used, whether they are networked or stand-alone, whether a security system is in place, and so on. Among the things your instructor will tell you will be the answers to the following questions:
About this Manual
In this (and every other) exercise, instructions will be printed in this
default font (the one you are reading).
To help you distinguish the instructions from what appears on your screen,
text that you should see displayed on your screen will be shown in
The distinction between typing something and entering something is as follows:
Getting Started with UNIXFollow your instructor's instructions for beginning a session. When you see a blinking rectangle sitting to the right of something like this: %or perhaps this: <1>~:or something similar, then your computer is ready for your session to begin. The text being displayed is called the system prompt -- a message from your computer's operating system (UNIX) that it is ready for you to enter your first command. (Your prompt is easy for a user to customize; as a result, the same UNIX system may display different prompts for different users.) Any time that you see this prompt, you can enter a command following it. Throughout this manual, we will use % to refer to the prompt.
Interacting with the ComputerNext, we will learn to interact with your computer. A UNIX interaction consists of three steps:
The commands we'll ask the computer to perform take different amounts of time to complete. Some will take a fraction of a second. Others may take a several minutes. Others will continue to execute indefinitely, until we specifically ask them to stop. The way in which you interact with a computer depends upon the computing environment of its operating system. For example, an environment in which you repeatedly:
Another common UNIX environment created at the Massachusettes Institute of Technology (MIT) is the X-window system. The X-window system provides an environment in which you use the mouse to interact with on-screen windows, menus and icons. Such environments are called graphical user interface, or GUI (pronounced gooey) environments. One of the kinds of windows available in the X-window environment (called an x-term) provides a separate command-line environment. The X-window environment is thus a combination environment, combining the convenience of a GUI with the power of a command-line, making it a favorite of computer scientists around the world. Since both plain UNIX and X-windows require knowledge of command-line commands, we will concentrate on them in this exercise. In order to use a command-line environment, you must learn those commands that the environment "understands." Let's first see what happens if you type something that the environment does notrecognize. Enter % qwertyHow does the system respond ? If you try other nonsense words, you'll find that the system responds in the same way. It's thus safe to make mistakes and mistype commands -- you won't harm the computer.
Directories and FilesVery soon, we'll be entering and running a simple C++ program. But when we are done, we'd like to be able to save your program somewhere "safe" -- somewhere that, if the power goes off, your program can still be retrieved. To save your program safely, the computer stores it on magnetic media (usually a hard disk) in a container called a file. During this course, we'll create dozens of files, and so some means of organizing them is needed. Just as the documents in a filing cabinet are often kept in manila folders, we can create a container in which to keep related files, called a directory. Let's do it! Enter % mkdir projectsThe mkdir command is the UNIX command to make-a-directory.
By placing the word projects after the mkdir command,
we tell the system that we want to make-a-directory named "projects."
Practice by using the mkdir command to make three directories:
one named "practice", one named "labs" and one named "myLib".
Viewing the Contents of a DirectoryNext, let's learn how to look at what's inside of a directory. Enter % lsThe ls command stands for list the contents of a directory.
If all is well, you should see (at least) four things listed:
the four directories you just created!
labs mylib practice projects(You may see more than these four directories, depending on how things are set up at your institution.) Many people like to visualize the relationships of these directories like this:
where
The % ls labsIf you follow the ls command with the name of a directory,
it displays that contents of that directory.
Since each of your directories are new (i.e., empty),
there is nothing for ls to display.
To get a more detailed listing of the current directory, enter % ls -lThe -l is called a switch that causes the ls
command to generate a long (i.e., detailed) listing.
The UNIX On-line Manual
Detailed information about all of the switches that can be used with
a given UNIX command (such as % man lsthe manual entry for ls will be displayed,
providing exhaustive information about that command,
including all of its switches, the meaning of the long listing information,
and other information.
Don't be discouraged if much of the manual does not yet make sense,
reading the UNIX manual is an acquired skill that requires much practice,
and you will become more accomplished at it over time.
Removing a DirectorySince we made practice just for practice, let's get rid of it. Enter % rmdir practiceThe rmdir command is the remove directory command, so this
changes our picture as follows:
Whatever directory you specify following the command will be removed
from the current directory, provided that the directory being removed
is empty.
To remove a non-empty directory named % rm -r DirectoryNamecan be used (but this should only be used when DirectoryName contains nothing important).
Using the
Identifying the Working DirectoryWhen you begin a computing session, UNIX always starts you out in the same place -- a special directory called your home directory. As we shall see shortly, you need not remain in your home directory, but can "change location" to a different directory.
The directory in which you are working at any given time is called your
working directory.
(Your home directory never changes, but your working directory can.)
To find out the name of the working directory, you can use the % pwd /home/adamsTry it for yourself and note what is displayed.
UNIX systems utilize what is called a hierarchical directory system,
meaning that directories can contain other directories, and that
there is one root directory named % ls /to list the contents of your system's root directory. Within / are a number of subdirectories, including
usr, sys, include, and so on,
each of which contains other directories and/or files.
On my UNIX system, the users' home directories are stored within
one of these subdirectories whose name is home.
Within home, the name of my home directory is adams.
Here is a picture showing a bit of the hierarchy on my machine:
Now, if you compare this to my output from
Apply this to the output from your
A last word on the home directory:
UNIX treats the tilde character (
Changing to a Different DirectoryIf we created all of our files in our home directory, it would soon become cluttered. By grouping related files within a directory, one's files can be kept more organized. Thus, we created labs in which we'll store the files for our lab exercises, and projects in which you can store your programming projects files (we'll discuss mylib in a later lab exercise.)
Since we are working on a lab exercise, let's change the working directory
to directory % cd labsThe cd command is the change directory command that
changes the working directory to the directory whose name follows the command.
Next, use the
The
Since To see everything in a directory, enter % ls -a
The In the UNIX file system, . is another name for the working directory (whatever directory you are in); and .. is another name for the parent of the working directory.
For example: We created % cd ..This command moves us "up" one directory because .. is always a synonym for the parent of the working directory, regardless of its actual name. We've thus changed the working directory back to what it was previously:
(Note that since that directory is our home directory, we could also have used
To wrap up this section, do the following:
|
Home Membership
|
|||||
|
Last update: Monday, August 21, 2000 at 3:38:27 PM. |
||||||