diff --git a/lab1/stream-examples/Makefile b/lab1/stream-examples/Makefile new file mode 100644 index 0000000..3221e68 --- /dev/null +++ b/lab1/stream-examples/Makefile @@ -0,0 +1,40 @@ +# Define the compiler and the linker. The linker must be defined since +# the implicit rule for linking uses CC as the linker. g++ can be +# changed to clang++. +CXX = g++ +CC = $(CXX) + +# Generate dependencies in *.d files +DEPFLAGS = -MT $@ -MMD -MP -MF $*.d + +# Define preprocessor, compiler, and linker flags. Uncomment the # lines +# if you use clang++ and wish to use libc++ instead of GNU's libstdc++. +# -g is for debugging. +CPPFLAGS = -std=c++11 -I. +CXXFLAGS = -O2 -Wall -Wextra -pedantic-errors -Wold-style-cast +CXXFLAGS += -std=c++11 +CXXFLAGS += -g +CXXFLAGS += $(DEPFLAGS) +LDFLAGS = -g +#CPPFLAGS += -stdlib=libc++ +#CXXFLAGS += -stdlib=libc++ +#LDFLAGS += -stdlib=libc++ + +# Targets +PROGS = read-words example-out + +all: $(PROGS) + +# Phony targets +.PHONY: all test clean distclean + +# Standard clean +clean: + rm -f *.o $(PROGS) + +distclean: clean + rm *.d + +# Include the *.d files +SRC = $(wildcard *.cc) +-include $(SRC:.cc=.d) diff --git a/lab1/stream-examples/example-out.cc b/lab1/stream-examples/example-out.cc new file mode 100644 index 0000000..b720963 --- /dev/null +++ b/lab1/stream-examples/example-out.cc @@ -0,0 +1,11 @@ +/* An example program that prints text to both standard out and standard error. + */ +#include + +int main() +{ + std::cout << "This text is written to stdout\n"; + std::cerr << "And this is written to stderr\n"; + std::cout << "More text to stdout\n"; + std::cerr << "And some more to stderr\n"; +} diff --git a/lab1/stream-examples/read-words.cc b/lab1/stream-examples/read-words.cc new file mode 100644 index 0000000..7a21993 --- /dev/null +++ b/lab1/stream-examples/read-words.cc @@ -0,0 +1,22 @@ +/* An example program that reads words from the standard input + * and prints them each on a separate line to standard out. + * + * If running this from the terminal, note two things: + * 1. Text is buffered and not sent to the program until + * you hit enter. + * + * 2. The program reads until it reaches the end of the stream. + * For the terminal, you need to send an end-of-file (EOF) + * character by pressing -d as the first character on + * a line. + */ +#include +#include + +int main() +{ + std::string s; + while(std::cin >> s){ + std::cout << s << '\n'; + } +}