Section 15.6 Reading from a File on the Server
So far all files to practice reading from have been provided for you via our convenient code windows. But if you wanted to read from a file that you yourself provide we’d reach the limitations of our code windows. Such work has to be done on the Thayer School servers (or with a C compiler installed on your computer). Let’s practice file reading on the Thayer School servers. Recall that in order to work on the Thayer School servers you need to be connected to the Dartmouth Secure Network via a VPN connection. If you haven’t already, be sure to start VPN.
Let’s go back to our first file reading example (see the code window below) and make it work on the servers. To do so, we need to save the code on the server: let’s call the program
fileRead0.c
, and we also need to save the data file on the server (that is the file containing the numbers): let’s call that file my_first_file.txt
. You can put the same numbers into the file that are in the file in the code window or you can pick any other 10 integer numbers and place them into a file.
Be sure to save both the source code and the data file inside the same directory. Then, log onto the server, navigate to the directory with these files, compile the source code using the command
compile fileRead0.c
and then run the code by simply typing fileRead0
at the command prompt.