Streams in C++

inserting an Image

inserting an Image

How to Get Input?

inserting an Image inserting an Image

Writing to ofstream

inserting an Image

inserting an Image inserting an Image inserting an Image inserting an Image inserting an Image inserting an Image

Examples

Reading txt file using ifstream

Example:

#include<iostream>
using std::endl;
#include<fstream>
using std::ifstream;
#include<vector>
using std::vector;
#include<string>
using std::string;



using std::cout;

int main(){

    ifstream file; // Create a File Object

    file.open("file.txt");

    vector<string> names; // vector to store the names
    string input; // where the names will be tempoary stored

    while( file >> input) // will evaluate as True if the read is sucessful
    {
        names.push_back(input);
    }

    for (string name : names ){

        cout << name << endl;
    }

}

File.txt

Devin Joseph Powers
Austin
John
Adams
Aaron
Michael

Output:

Devin
Joseph
Powers
Austin
John
Adams
Aaron
Michael

Another Way to do Input

  • use .get()

#include <iostream>
#include <string>
#include <utility>
#include <fstream>
#include <algorithm>
using std::string; 
using std::ifstream; using std::cout; using std::endl; using std::to_string;
using std::invalid_argument;

// create custom data struture here



void ParseFileData(const string &fname){


    ifstream inFile(fname);   //ifstream is the txt file object?

    bool result;

    //error handling for no file found
    if(!inFile){
        throw std::invalid_argument("invalid argument DUDE!!");
    }

    if (inFile.is_open()){

        string username, command, server_name;

        while (inFile >> username >> command >> server_name ){ //each name/command one at a time

            if ( command != "join" && command != "leaving")
            {
                cout << "ERRORRRRR!!!" << endl;
            }
            // else tho
            if ( command == "join")
            {
                cout << "Joining:  " << username << " : " << server_name << endl;
                //result = //send to add connection
            }
            else if (command == "leaving"){

                cout << "Leaving " << username << " : " << server_name << endl;
            }

        }
        
    }
    inFile.close(); // close file

}

int main(){

    // send file to our function
    ParseFileData("file.txt");
}

file.txt

devin join server1
kobe join server2
lebron join server1
kobe join server2
paul leaving server3
ryan leaving server3
tommas join server1

Output:

Joining:  devin : server1
Joining:  kobe : server2
Joining:  lebron : server1
Joining:  kobe : server2
Leaving paul : server3
Leaving ryan : server3
Joining:  tommas : server1