Lab 1 — File I/O and X11 Basics

CMPS 3600 • Fall 2025

Overview

This lab practices file I/O in C/C++ and introduces X11 graphics. We will write a small C++ program, refactor it to ANSI C, and then build and modify an X11 windowed C program. Steps duplicate only what is new beyond Lab 0.

Step 1 — Setup (if needed)

If your ~/cs3600/1/ folder is not set up, run the instructor scripts. Otherwise, skip to Step 2.

$ cd
$ /home/fac/dfanucchi/public_html/build_dirs.sh

Step 2 — C++ File I/O then Refactor to C

  1. Work in ~/cs3600/1/:
    $ /home/fac/gordon/p/3600/lab-start.sh
  2. Create lab1.cpp that:
    • Prompts for the user's name and a number
    • Opens a file named log
    • Writes the user's name
    • Calculates the summation from 1..N
    • Writes the total, closes the file
  3. Build & run:
    $ g++ lab1.cpp -Wall
    $ ./a.out
  4. Refactor to C:
    $ cp lab1.cpp lab1.c

Step 3 — X11 Program (most important)

Log out and log back in with X11 forwarding and compression so GUI windows can display on your machine.

$ <CTRL-D> # disconnect from Odin
$ ssh -YC YOUR_USER@odin.cs.csub.edu

The options mean: -Y = trusted X11 forwarding, -C = compress data.

Note: If you are using a personal laptop or non-CSUB device, you must have X11 graphics configured locally. See the setup guide here: Laptop X11 Setup Instructions.

  1. Copy and build the starter X11 program:
$ cd ~/cs3600/1
$ cp ~gordon/public_html/3600/examples/1/xwin89.c .
$ gcc xwin89.c -Wall -lX11
$ ./a.out

You should see a small window appear.

Required Modifications

Hint: You will need to select and handle input events in the X11 event loop.

Step 4 — Makefile

Add lab1.c and xwin89.c targets so make builds both. Example:

CXX := g++
CC  := gcc
CXXFLAGS := -std=c++17 -Wall -Wextra -O2
CFLAGS   := -std=c17   -Wall -Wextra -O2

all: lab1 xwin89

lab1: lab1.cpp
	$(CXX) $(CXXFLAGS) -o $@ $<

lab1-c: lab1.c
	$(CC) $(CFLAGS) -o $@ $<

xwin89: xwin89.c
	$(CC) $(CFLAGS) -o $@ $< -lX11

clean:
	rm -f lab1 lab1-c xwin89

Test:

$ make clean
$ make
$ make   # second time should say "Nothing to do"

Files to Submit / Check

Lab will be collected 9/2/2025 @ 11:59 PM