Added standard I/O stream examples

This commit is contained in:
Sven Gestegard Robertz 2022-12-08 15:43:15 +01:00
parent c2103cdc4c
commit 8b87ead3b9
3 changed files with 73 additions and 0 deletions

View file

@ -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)

View file

@ -0,0 +1,11 @@
/* An example program that prints text to both standard out and standard error.
*/
#include <iostream>
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";
}

View file

@ -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 <Ctrl>-d as the first character on
* a line.
*/
#include <iostream>
#include <string>
int main()
{
std::string s;
while(std::cin >> s){
std::cout << s << '\n';
}
}