11.28.2021 Mini C++ Lesson

Reading a file

Output

C:\clionUtilities\fileReader\cmake-build-debug\fileReader.exe
Enter file to read: 
C:\clionUtilities\testFiles\testFile.txt
000000011111110000000
000000011111110000000
000000011111110000000
111111111111111111111
111111111111111111111
111111111111111111111
000000011111110000000
000000011111110000000
000000011111110000000
000000011111110000000
000000011111110000000
000000011111110000000

Process finished with exit code 0

testFile.txt

000000011111110000000
000000011111110000000
000000011111110000000
111111111111111111111
111111111111111111111
111111111111111111111
000000011111110000000
000000011111110000000
000000011111110000000
000000011111110000000
000000011111110000000
000000011111110000000
#include <iostream>
#include <fstream>

using namespace std;

std::string readFile(std::string filename) {
    string line;
    ifstream fileReadObject;
    fileReadObject.open(filename);
    if (fileReadObject.is_open()) {
        while (getline(fileReadObject,line)) {
            std::cout << line << std::endl;
        }
    }
}

int main() {
    std::cout << "Enter file to read: " << std::endl;
    std::string filenameInput;
    std::cin >> filenameInput;
    readFile(filenameInput);
    return 0;
}

From a guy that worked on Microsoft J++ before 2000

Open GL in 2003 or so

Free Image

Free Image

Output
15 eagles 20 Dragon 19 bears 9 marmaset

circusCombinatorics.txt
1 cats
2 dogs
3 bugs
4 spiders
5 bats
6 birds
7 fish
8 vultures
9 marmaset
10 donkeys
11 lions
12 tigers
13 frogs
14 lizards
15 eagles
16 pythons
17 cobras
18 rattle snake
19 bears
20 Dragon

Updated main.cpp

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <array>
#include <vector>

using namespace std;

std::string readFile(std::string filename) {
    string line;
    ifstream fileReadObject;
    fileReadObject.open(filename);
    if (fileReadObject.is_open()) {
        while (getline(fileReadObject,line)) {
            std::cout << line << std::endl;
        }
    }
}

std::vector<string> readCombinatoricsD20File(std::string filename) {
    string line;
    std::vector<string> lines;
    ifstream fileReadObject;
    fileReadObject.open(filename);
    int i = 0;
    if (fileReadObject.is_open()) {
        while (getline(fileReadObject,line)) {
            std::cout << line << std::endl;
            lines.push_back(line);
            i++;
        }
    }
    return lines;
}

int printRandomNumberD20() {
    int randomNumber;
    srand(time(0));
    for (int i = 0; i < 10; i++) {
         randomNumber = rand() % 20;
         cout << randomNumber << " ";
    }
    cout << endl;
    return randomNumber;
}

int getD20() {
    int randomNumber;
    randomNumber = rand() % 20;
    return randomNumber;
}

string getD20Value(vector<string> lines) {
    int lineToRead = getD20();
    return lines[lineToRead];
}

int main() {
    srand(time(0));
    printRandomNumberD20();
    vector<string> lines = readCombinatoricsD20File("C:\\clionUtilities\\testFiles\\circusCombinatorics.txt");
    int lineToRead = printRandomNumberD20();
    for (int i=0; i<4; i++) {
        std::cout << getD20Value(lines) << " ";
    }
    std::cout << endl;
    return 0;
}

EagleDragonBearMarmaset first roll

9 marmaset 4 spiders 3 bugs 17 cobras Silver roll

MarmasetSpiderBugCobra second roll

Real dice roll time

7 10 18 7

FishDonkeyRattlesnakeFish

SlowStubbornMantaRay

https://www.geeksforgeeks.org/array-strings-c-3-different-ways-create/

stack exchange has some help as well

Free Image

Rusty Spotted Marma Eagle as Prep Work

Free Image

Silver Spider Cockroach with Cobras for arms

Free Image
Example output

C:\clionUtilities\fileReader\cmake-build-debug\fileReader.exe
10 3 14 4 15 14 14 11 12 10
1 cats
2 dogs
3 bugs
4 spiders
5 bats
6 birds
7 fish
8 vultures
9 marmaset
10 donkeys
11 lions
12 tigers
13 frogs
14 lizards
15 eagles
16 pythons
17 cobras
18 rattle snake
19 bears
20 Dragon
10 3 14 4 15 14 14 11 12 10
9 marmaset 4 spiders 3 bugs 17 cobras

Process finished with exit code 0

CLion from JetBrains with MinGw C++ Compiler

Published by techinfodebug

Flex and Java Developer, Christian, Art, Music, Video, and Vlogging

Leave a comment