Intro Lab Session
In this lab you will:
- Access a computer remotely
- Get strings from the command line
- Read from an input file into a string
- Count some types of characters in the input file
- Test your code
Important Note
The ssh and scp commands described below
will only work from within the CS network.
If you are outside the CS network,
you need to use the CS VPN to access the CS network.
Instructions for using the VPN are found here:
https://support.cs.byu.edu/KB/View/77652585-connecting-to-the-globalprotect-paloalto-vpn
Part 1: Remote Access
-
Open a terminal window
If you're using a Windows system run either the "Windows PowerShell" app or the "Command Prompt" app. For a MacOS or Linux system run the "Terminal" app.
-
Login to a CS lab machine
The "ssh" command is used to login to another machine. Replace "user" in the command below with your BYU netid. Type your BYU CS account password at the 'Password:' prompt. (note there is no feedback while you type the password)
$ ssh user@schizo.cs.byu.edu
If you have trouble with your CS account, look first at:
https://docs.cs.byu.edu/doku.php?id=setting-up-your-account-with-the-cs-authentication-system
If that doesn't solve the problem, submit a ticket at:
https://support.cs.byu.edu
The System Administrators may be contacted in-person in 1140 TMCB or at system@cs.byu.edu if needed. -
Make a file (hello.cpp)
The "nano" command allows you to write code in a file. (You are welcome to use another editor if you prefer.)
$ nano hello.cpp
Enter the following code into the file:
#include <iostream> using namespace std; int main() { cout << "Hello" << endl; }
-
Compile the code (hello.cpp)
The "g++" command runs the C++ compiler and produces an executable file.
$ g++ -Wall -std=c++17 -g hello.cpp -o lab0
-
Run the code
The name of the executable file is "lab0" and "./" allows the terminal to find the file in the current folder.
$ ./lab0
The output should look something like this:
Hello
Part 2: Command Line Parameters
-
When running a program such as "lab0" in the previous example, the strings typed on the command line are passed into the "main" function of your program. This functionality is often used to pass a file name to the program.
The following declaration of the "main" function shows the parameters "argc" and "argv" that are passed to the function when it is called.
int main(int argc, char* argv[]) { ... }
Suppose the program is run from the command prompt and a file name is also given on the command line like this:
$ ./lab0 filename.txt
In this case, the values passed to main would be 2 for "argc", ./lab0 for "argv[0]", and filename.txt for "argv[1]".
-
Add code to your main function to print "argc" and "argv[0]" and "argv[1]".
Compile and run.
$ g++ -Wall -std=c++17 -g hello.cpp -o lab0 $ ./lab0 filename.txt
The output should look something like this:
argc: 2 argv[0]: ./lab0 argv[1]: filename.txt
-
When we run your code for the projects in this class, we'll give the name of the input file on the command line. Your code should use "argv[1]" to get the name of the input file.
Part 3: Reading from a file
-
One way to read input files is to read the entire input file into a string. The nice thing about this approach is that you can use string operations to process the input which is often easier than using file operations. This approach could cause problems for large files, but we don't expect to use input files that are too large.
-
Add the following file reading code to your main function. This code reads from a file and puts the text that was read from the file in a string variable named "input". The variable "filename" should be a string variable and should contain the name of the file you want to read. (You could get that from "argv[1]".) You'll also need #include's for "fstream" and "sstream".
ifstream in; in.open(filename); stringstream ss; ss << in.rdbuf(); string input = ss.str(); in.close();
-
Add some code to print the text that was read.
First print the line:
*** Start of Input ***
Then print the "input" string that was read.
Finally print the line:
*** End of Input ***
Compile and run. (If you need a file to test with you might use "hello.cpp")
The output should look something like this:
*** Start of Input *** (lines from the file that was read) *** End of Input ***
-
Take a screenshot showing your terminal and the resulting output.
Part 4: Counting Characters
-
Some kinds of input characters will be important for the projects in this class. One of these is the newline character. You will need to recognize newline characters so you can keep track of line numbers. In the C++ language the newline character is represented with the following character literal.
'\n'
Other kinds of characters you will need to recognize in the input are whitespace characters, digits, and letters. The C++ library has the following functions that will help you recognize these kinds of characters.
isspace(c) isdigit(c) isalpha(c)
The "isspace" function returns "true" when 'c' is a whitespace character.
The "isdigit" function returns "true" when 'c' is a digit.
The "isalpha" function returns "true" when 'c' is a letter.
To use these functions you need to include the "cctype" header file.
-
Add code to your program to count the total number of characters in the input, as well as the number of newline characters, whitespace characters, digits, and letters. Print the each of these counts to the output.
Compile and run.The output should look something like this:
(Your counts will be different. They depend on the file you are reading.)chars: 32 lines: 4 wspace: 8 digits: 2 letters: 16
-
Take a screenshot showing your terminal and the resulting output.
Part 5: Test Your Code
-
We have prepared some files for testing your character counting. Download the Lab 0 test files from this link:
-
Copy the Lab 0 files to a CS lab machine
The "scp" command is used to copy files to another machine. (The "scp" below is copying from your local computer to the remote. You should run it in a terminal window on your local computer.) Replace "user" in the "scp" command below with your BYU netid. Type your BYU CS account password at the 'Password:' prompt. (note there is no feedback while you type the password)
$ scp lab0-tests.zip user@schizo.cs.byu.edu: (You should run "scp" on the local computer.) (Don't forget the colon ':' character on the end.)
-
Unpack the files (on the remote machine)
$ ssh user@schizo.cs.byu.edu $ unzip lab0-tests.zip (You should run "unzip" on the remote computer.)
-
Run your code and capture the output
$ ./lab0 lab0-tests/input01.txt >actual.txt
-
Compare the expected output with the actual output
Each input file has an expected output file. Your output should have the same counts as the expected output. The "diff" command will tell you what's different. (There is no output when the files are the same.)
$ diff lab0-tests/answer01.txt actual.txt
-
Run all the tests
The file "run-tests.sh" contains a script that will run all the inputs and compare the actual outputs with the expected outputs.
$ sh run-tests.sh
-
Take a screenshot showing your terminal and the resulting output.
Part 6: Submission
-
Zip your code (on the remote machine)
The "zip" command below packages all .h files and .cpp files into a single zip file. (You probably don't have any .h files for this lab, but this command will work for other labs where you do have .h files.)
$ zip lab0-code.zip *.h *.cpp (You should run "zip" on the remote computer.)
-
Copy the zip file to your local computer
The "scp" command below is copying from the remote to the local computer. You should run it in a terminal window on your local computer. Replace "user" in the "scp" command below with your BYU netid.
$ scp user@schizo.cs.byu.edu:lab0-code.zip . (You should run "scp" on the local computer.) (Don't forget the period '.' character on the end.)
-
Submit your screenshots and the zip file containing the code you wrote during this session to Learning Suite.