CS120 Day03: Warm-up, I/O, git intro
---------------------------------------
>>>> Write html safe color codes program <<<<
We are going to create the html to produce a safe color code chart. A
color is determined by setting values for Red, Green and Blue
(RGB). Possible values are from [0,255] which correspond to
hexadecimal (base 16) values 00-FF. Colors can be indicated in
several ways, include a string of hex digits preceded by a '#'. In
the past only certain hex values would render consistently in all
browsers - those are considered "safe" color codes and have repeating
digits for RGB values: 00, 33, 66, 99, FF, CC.
(http://www.w3schools.com/html/html_colors.asp)
Step 1) Write program to output all possible safe color codes using only 0,
3, 6, 9, C, F hex characters, one color per line.
http://www.cs.jhu.edu/~joanne/cs120/notes/colorChart1.c
Step 2) Wrap your codes (RRGGBB) in an html font color command: RRGGBB where the RRGGBB codes match.
Step 3) Add and to the beginning and end of your
output. Use Unix redirection to save the output to a file called
"codes.html". Open codes.html with a web browser (how?).
Step 4) Decide on a table format for your color codes. Format your
table surrounded by
tags. Each line of the table
starts and ends with
tags. Each element of the table
should be enclosed in | tags.
>>> See posted solutions on Piazza <<<
I/O FUNCTIONS & FILES -> See James' notes and examples
A few more details:
scanf - formatted keyboard input (similar to printf)
- conversion specifications (printf & scanf)
%d integer
%ld long
%c char
%s string
%f float (real number type)
%lf double, Lf long double
- scan set: [] notation - inputs as long as chars in set, [^] inverse
- use character literals to skip them, eg "%d-%d-%d"
- use %*c to skip any character
- use %*d to skip an integer
- use * to skip assigning any value
int number;
float x;
double y;
char word[20];
scanf("%d", &number);
scanf("%f %lf", &x, &y);
scanf("%s", word);
- don't need &
- don't use subscript
- only reads up to first whitespace (space, tab, ret)
- make sure memory is allocated & big enough
SEQUENTIAL FILES
-----------------------
- declare: FILE *fptr;
- must open file for use, giving it pointer, name & open mode
fptr = fopen("name.txt", "w") (returns NULL if error)
- types of file openings:
"r" - reading
"w" - create or overwrite file for writing
"a" - open or create file for appending to end
"r+" - open for update (read and/or write)
"w+" - create (overwrite) file for read &/or write
"a+" - open for read or write to end
- close a file when done: fclose(fptr);
- test for end of file: feof(fptr) returns 1 if end, 0 otherwise
- fflush(fptr) - flush the buffer - usually for output
- rewind(fptr) - set to beginning of file
- I/O functions we learned have file specific equivalents
fgetc(stdin) = getchar()
fputc('c', stdout) = putchar('c')
?? fgets(str, stdin) = gets(str)
fputs(str, stdout) = puts(str)
- fprintf(fptr, same as printf);
- fscanf(fptr, same as scanf);
GIT basics -> See James' notes
MORE UNIX TOOLS =============================================
(we'll get to this unix stuff eventually - read and experiment in the meantime)
- viewing files: cat, more, less
- basic file commands: ls, mkdir, cd, cp, mv, rm
- printing: lp filename (check what default printer is)
- compile & run
>gcc file.c -> creates a.out -> run with ./a.out
>gcc -o file file.c -> creates executable file -> run with ./file
- zipping files together: we create a "tarball" and use the
compression option (z)
>tar zcvf pg1.tar.gz pg1 zips up all the files in directory pg1
>tar zxvf pg1.tar.gz extracts all the files in the zip
- downloading a file from the web: wget http://someURL
- several "shell" options: bash, tcsh, etc.
- customize your environment with various login files depending on the
shell you choose to use: .login, .cshrc, .bashrc, .tshrc, etc.
- can set prompt:
set prompt="joanne> "
- can set default editor:
setenv EDITOR emacs
- can create aliases:
alias clear
alias c clear
alias e emacs
alias ls 'ls -F'
alias ugrad 'ssh -l joanne ugrad4.cs.jhu.edu'
alias gcc120 'gcc -std=c99 -pedantic -Wall -Wextra -O'
alias g++120 'g++ -std=c++98 -pedantic -Wall -Wextra -O'