Skip to content

Commit 03fcdb0

Browse files
committed
feat: streams
1 parent 2ac5068 commit 03fcdb0

7 files changed

Lines changed: 44570 additions & 4 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ time for you to revisit them.
1010
2. [Developer Portfolio](portfolio/README.md)
1111
3. [Artificial Intelligence](artificialintelligence/README.md)
1212

13+
# Philosophy
14+
15+
Before we start, this repository aims to be practical, and it will be updated as I test the methodology. Frame it as a guidebook, not a manual. Most of the time, I am constrained by the time, so in order to move fast, I don't cover deeply some topics, but the basics that allows you to explore by yourself or point the directions for you to study in other places acting as a self-taught student, so you really should look for more information elsewhere if you feels so. I use lots of references and highly incentive you to look for other too and propose changes in this repo. Sometimes, I will mostly try to teach in a chaotic way, which implies that you will need to explore the concepts by yourself or read the manual/books. Every student should follow your own path to learning, it is impossible for me to cover every learning style, so it is up to you to build your own path and discover the best way to learn. What worked for me or what works for a given student probably wont work for you, so dont compare yourself to others too much, but be assured that I am here to help you to succeed. If you need help, just send me a message in private, or use public forums such as github issues and discussions.
16+
1317
# Badges
1418

1519
CI: [![Documentation](https://github.com/InfiniBrains/Introduction-to-Game-Programming-With-CPP/actions/workflows/documentation.yaml/badge.svg)](https://github.com/InfiniBrains/Introduction-to-Game-Programming-With-CPP/actions/workflows/documentation.yaml)

intro/01-introduction/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Reasons why you should learn how to program with C++
22

3+
Before we start, this repository aims to be practical, and I highly incentive you to look for other references. I want to add this another [awesome repository](https://github.com/federico-busato/Modern-CPP-Programming/) it holds an awesome compilation of modern C++ concepts.
4+
5+
Another relevant reference for what we are going to cover is the [updated core guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) that explain why some syntax or style is bad and what you should be doing instead.
6+
37
## Why?
48

59
The first thing when you think of becoming a programmer is **HOW DO I START?** Well, **C++** is one of the best

intro/06-functions/README.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ int main()
155155
156156
# Passing parameter to a function by reference
157157
158-
Pass-by-reference occurs when the function parameter uses the `&` in the parameter declaration. It will allow the function to modify the value of the parameter directly in the other scope, rather than making a copy of the value as it does with pass-by-value.
158+
Pass-by-reference occurs when the function parameter uses the `&` in the parameter declaration. It will allow the function to modify the value of the parameter directly in the other scope, rather than making a copy of the value as it does with pass-by-value. The mechanism behind the variable passed is that it is an alias to the outer variable because it uses the same memory position.
159159
160160
```c++
161161
#include <iostream>
@@ -172,6 +172,73 @@ int main() {
172172
}
173173
```
174174

175+
# Passing parameter to a function by pointer
176+
177+
Pass-by-pointer occurs when the function parameter uses the `*` in the parameter declaration. It will allow the function to modify the value of the parameter in the other scope via memory pointer, rather than making a copy of the value as it does with pass-by-value. The mechanism behind it is to pass the memory location of the outer variable as a parameter to the function.
178+
179+
```c++
180+
#include <iostream>
181+
using namespace std;
182+
void times2(int *x) { // by using *, x has the same address the variable passed where the function is called
183+
// x holds the address of the outer variable
184+
// *x is the content of what x points.
185+
*x *= 2; // it will change the variable in caller scope
186+
}
187+
188+
int main() {
189+
int y = 2;
190+
times2(&y); // the function expects a pointer, given pointer is an address, we pass the address of the variable here
191+
cout << y << endl; // Outputs 4
192+
return 0;
193+
}
194+
```
195+
196+
# Function overload
197+
198+
A function with a specific name can be overload with different not implicitly convertible parameters.
199+
200+
```c++
201+
#include <iostream>
202+
using namespace std;
203+
204+
float average(float a, float b){
205+
return (a + b)/2;
206+
}
207+
208+
float average(float a, float b, float c){
209+
return (a + b + c)/3;
210+
}
211+
212+
int main(){
213+
cout << average(1, 2) << endl; // print 1.5
214+
cout << average(1, 2, 3) << endl; // print 2
215+
return 0;
216+
}
217+
```
218+
219+
# Default parameter
220+
221+
Functions can have default parameters that should be used if the parameter is not provided, making it optional.
222+
223+
```c++
224+
#include <iostream>
225+
using namespace std;
226+
227+
void greet(string username = "user") {
228+
cout << "Hello " << mes << endl;
229+
}
230+
231+
int main() {
232+
// Prints "Hello user"
233+
greet(); // the default parameter user is used here
234+
235+
// Prints "Hello John"
236+
printMessage("John");
237+
238+
return 0;
239+
}
240+
```
241+
175242
# Scopes
176243
177244
Scope is a region of the code where a identifier is accessible. A scope usually is specified by what is inside `{` and `}`. The global scope is the one that do not is inside any `{}`.

intro/07-streams/README.md

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,51 @@ At this point, you already are familiar with the `iostream` header. But we never
44

55
But C++ have 2 other relevant streams that we need to cover: [`fstream`](https://en.cppreference.com/w/cpp/header/fstream) and [`sstream`](https://en.cppreference.com/w/cpp/header/sstream).
66

7+
# File streams
8+
9+
File streams are streams that target files instead of the terminal console. The [`fstream` header](https://en.cppreference.com/w/cpp/header/fstream) describes the file streams and the ways you can interact with it.
10+
11+
The main differences between console and file streams are:
12+
- You have to target the filesystem path for files because we can manage different files at the same, but for console, you only have one, so you dont need to target which console we are streaming. In order to not mess each target, you have to declare a different variable to store the target and state.
13+
- Files are persistent, so if you write something to them, and try to read from it again, the that will be there saved.
14+
15+
Files are a kind of resource managed by the operation system. So every time you request something to be read or write, behind the scenes you are requesting something to the operating system, and it can be slow or subject by lock control. When you open a file to be read or write, the OS locks it to avoid problems. You can open a file to be read multiple times simultaneously, but you cannot write more than once. So to avoid problems, after reading or writing the file, you should close the file.
16+
17+
```c++
18+
#include <fstream>
19+
#include <iostream>
20+
#include <string>
21+
22+
using namespace std;
23+
24+
int main() {
25+
// Open the file
26+
// this file path is relative to the executable, so be assured it exists in the same folder the executable is placed
27+
// fin is the variablename and it is function initialized via a file path to target, but it can be any valid identifier
28+
// I am using fin as variable to follow the same metaphor `fin` as `file input` as we have with console input `cin`,
29+
ifstream fin("file.txt");
30+
31+
// Check if the file is open
32+
// it is a good practice to check if the file is really there before doing anything
33+
if (!fin.is_open()) {
34+
cerr << "Error opening file" << endl;
35+
return 1; // quits the program with an error code
36+
}
37+
38+
// Read the contents of the file line by line
39+
string line;
40+
// getline can target streams in general, so you can pass the file stream as a target
41+
while (getline(fin, line)) { // while the file have lines, read and store the content inside the line variable
42+
cout << line << endl; // output each string into the console
43+
}
44+
45+
// Close the file
46+
fin.close();
47+
48+
return 0;
49+
}
50+
```
51+
752
# String Stream
853

954
The [`sstream` header](https://en.cppreference.com/w/cpp/header/sstream) describes string stream, which is a type of memory stream and is very useful to do string manipulation. For our intent, we aro going to focus 3 types of memory streams.
@@ -47,6 +92,71 @@ int main() {
4792
}
4893
```
4994

50-
# File streams
95+
You can combine string stream and file stream to read a whole file and store into a single string.
96+
97+
```c++
98+
#include <fstream>
99+
#include <iostream>
100+
#include <sstream>
101+
#include <string>
51102

52-
File streams are streams
103+
using namespace std;
104+
105+
int main() {
106+
// Open the file
107+
ifstream file("file.txt");
108+
109+
// Check if the file is open
110+
if (!file.is_open()) {
111+
cerr << "Error opening file" << endl;
112+
return 1;
113+
}
114+
115+
// Read the contents of the file into a stringstream
116+
stringstream ss;
117+
ss << file.rdbuf(); // read the whole file buffer and stores it into a string stream
118+
119+
// Close the file
120+
file.close();
121+
122+
// Convert the stringstream into a string
123+
string contents = ss.str();
124+
125+
cout << contents << endl; // prints the whole file at once
126+
127+
return 0;
128+
}
129+
```
130+
131+
# Homework
132+
133+
You have the job of creating a small program to read a file image in the format PGMA and inverse the colors as a negative image.
134+
135+
You can test your code with different images if you want. You can download more images [here](https://people.sc.fsu.edu/~jburkardt/data/pgma/pgma.html). But here goes 2 examples:
136+
137+
- Sample input easy: [baboon.ascii.pgm](baboon.ascii.pgm) - max intensity is not 255 and don't have comments.
138+
- Sample input harder: [lena.ascii.pgm](lena.ascii.pgm) - have comments, and the max intensity is different than 255.
139+
140+
You can test if your output file is correct using [this tool](https://www.gimp.org/). You can open this file via any text reader, use the online viewer, or use any app that reads pnm images.
141+
142+
## Attention:
143+
144+
- To create the inverse image, you should read the file header and search for the maximum intensity. You should use this number as a base to inverse. In the Lena case, it is 245.
145+
- You should pay attention that every line shouldn't be bigger than 70 chars;
146+
- Pay attention that the line 2 might exists or not. And any comment found in the file should be skipped.
147+
148+
The user should input the filename to be read. So you should store it into a string variable. The output filename should be the same as the input but with '.inverse' concatenated in the end. Ex.: lena.pgm becomes lena.inverse.pgm; If you find this too complicated, just concatenate with .inverse.pgm would be acceptable. ex.: lena.pgm becomes lena.pgm.inverse.pgm
149+
150+
In order for your program to find the file to be read, you should provide the fullpath to the file or simply put the file in the same folder your executable is.
151+
152+
HINT:
153+
In order to find comments and ignore them do something like that:
154+
```c++
155+
string widthstr;
156+
int width;
157+
fin >> widthstr;
158+
if(widthstr.at(0)=='#')
159+
getline(fin, widthstr); // ignore line
160+
else
161+
width = stoi(widthstr); // covert string to integer
162+
```

0 commit comments

Comments
 (0)