Nieuws:

Welkom, Gast. Alsjeblieft inloggen of registreren.
Heb je de activerings-mail niet ontvangen?

Auteur Topic: Pointers in c++, Hoe moet ik deze declareren?  (gelezen 862 keer)

Offline bart85

  • Lid
Pointers in c++, Hoe moet ik deze declareren?
« Gepost op: 2014/11/20, 10:58:52 »
In mijn programma word de input via een pipe of bestand (als argument op de commandline) gegeven. Om dubbele code te verkomen wil ik met een pointer werken. Deze moet bij een pipe naar cin worden verwezen en in het geval van een bestand op de commandline meegegeven naar een ifstream.
Hoe declareer ik deze pointers?
Het gaat om een pointer die naar cin of naar een ifstream word verwezen.

int main (int argc, char* argv[])
{
string lineInput;
char *file = NULL;


for (int i=1; i < argc; i++)
{
if (i < argc -1 && string(argv[i]) == "-f"){file = argv[i+1];}
}
if (!file == NULL){ifstream fin (file);fin.open();input = &fin;}
else{input = &cin;}

while (getline(*input,lineInput)) {
if (iscode(lineInput)) {
cout << lineInput << endl;}}


if (!file == NULL){fin.close();}
}
Je leert maar mooi over weg gaan met de commandline. Geen grafische toepassingen voor systeembeheer.
You can never make a system 100% secure unless you unplug the machine from all networks, turn it off, lock it in a safe, smother it in concrete and never use it.

Re: Pointers in c++, Hoe moet ik deze declareren?
« Reactie #1 Gepost op: 2014/11/20, 11:09:51 »
Het type van std::cin is std::istream. Dit is ook het basistype van std::ifstream. Het is trouwens niet nodig om expliciet std::ifstream::open() en std::ifstream::close() aan te roepen -- als je een filename meegeeft aan de constructor wordt de stream automatisch geopend, en de destructor sluit deze dan weer. http://en.cppreference.com/w/cpp/io/basic_ifstream

Mag ik je trouwens aanraden om je code netjes geformatteerd te houden? Dat leest namelijk makkelijker en is ook eenvoudiger om later aanpassingen aan te doen:

int main(int argc, char* argv[]) {
    std::string lineInput;
    char* file = NULL;

    for(int i = 1; i < argc; i++) {
        if(i < argc - 1 && std::string(argv[i]) == "-f") {
            file = argv[i + 1];
        }
    }

    if(!file == NULL) {
        std::ifstream fin(file);
        input = &fin;
    } else {
        input = &cin;
    }

    while(std::getline(*input, lineInput)) {
        if(std::iscode(lineInput)) {
            std::cout << lineInput << std::endl;
        }
    }
}

I use a Unix-based system, that means I'll get laid as often as I have to reboot.
LibSylph
SeySayux.net

Offline bart85

  • Lid
Re: Pointers in c++, Hoe moet ik deze declareren?
« Reactie #2 Gepost op: 2014/11/20, 17:34:01 »
Ik heb de code geformatteerd. Er is code aan toegevoegd. Probleem is dat er geen weergave is als met de -f optie een file word meegegeven als argument. bij geen file en dus input van een pipe komt, is er wel weergave.
Hier de code:
/*dit programma filterd commentaar van configuratiebestanden eruit.
opties voor language
lscode [-l language] [-f file]

Werkt nog niet met inline commentaar.
*/
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

string language = "conf";

// deze variabele word false indien commentaar met /* .. */ (hiermee kan commentaar meerdere regels zijn)
bool r_cpp = true;

//iscode geeft false terug als een lijn commentaar is.
bool iscode (string line)
{
bool r = true;
int l;
l = line.length();
if (language == "conf")
{
if (l >= 1 && (line[0]=='#' || line[0] == ';'))
{
r = false;
}
else
{
r = true;
}
}
else if (language == "c++")
{
if (l >= 2)
{
if (line[0] == '/' && line[1] == '/'){r = false;}
if (line[0] == '/' && line[1] == '*'){r_cpp = false;}
if(line[l-2] == '*' && line[l-1] == '/'){r = false;r_cpp = true;}
if (r_cpp == false){r = false;}
}
}

if (line.empty())
{
r = false;
}
return r;
}

string usage (string programname)
{
string use;
use = "Usage: " + programname + " [-l language] [-f file]";
return use;
}

int main (int argc, char* argv[])
{
string lineInput;
char* file = NULL;
istream * input = NULL;

// weergave van het gebruik van het programma met optie --help
if (argc > 1 && string(argv[1]) == "--help"){cout << usage(argv[0]) << endl; return 0;}

// Argumenten van commandline inlezen.
for (int i=1; i < argc; i++)
{
if (string(argv[i]) == "-l" && i < argc - 1 )
{
i += 1;
language = string(argv[i]);
}
else if (string(argv[i]) == "-f" && i < argc -1)
{
i += 1;
file = argv[i];
}
}

// Als geen file als argument word gegeven, dan input van pipeline
if (file == NULL)
{
input = &cin;
}
else
{
//betand inlezen.
ifstream fin (file);
input = &fin;
}
while (getline(*input,lineInput))
{
if (iscode(lineInput))
{
cout << lineInput << endl;
}
}
}
Je leert maar mooi over weg gaan met de commandline. Geen grafische toepassingen voor systeembeheer.
You can never make a system 100% secure unless you unplug the machine from all networks, turn it off, lock it in a safe, smother it in concrete and never use it.

Re: Pointers in c++, Hoe moet ik deze declareren?
« Reactie #3 Gepost op: 2014/11/20, 22:08:33 »
Je maakt de stream fin aan binnen het else-block. Objecten worden op het einde van het blok waarin ze gedeclareerd zijn vernietigd. Je pointer wijst dus naar een dood object. Dat dit uberhaupt werkt en niet gewoon crasht is toeval.
I use a Unix-based system, that means I'll get laid as often as I have to reboot.
LibSylph
SeySayux.net

Offline bart85

  • Lid
Re: Pointers in c++, Hoe moet ik deze declareren?
« Reactie #4 Gepost op: 2014/11/21, 09:14:37 »
Hoe gaan andere programma's hier mee om? Ik weet niet hoe ik dit kan oplossen.
Je leert maar mooi over weg gaan met de commandline. Geen grafische toepassingen voor systeembeheer.
You can never make a system 100% secure unless you unplug the machine from all networks, turn it off, lock it in a safe, smother it in concrete and never use it.

Offline bart85

  • Lid
Re: Pointers in c++, Hoe moet ik deze declareren?
« Reactie #5 Gepost op: 2014/11/21, 11:04:13 »
Dit is opgelost door:
void erase_comment(istream *input)
{
string lineInput;
while (getline(*input,lineInput))
{
if (iscode(lineInput))
{
cout << lineInput << endl;
}
}
}

if (file == NULL)
{
erase_comment(&cin);
}
else
{
//betand inlezen.
ifstream fin (file);
erase_comment(&fin);
}
Je leert maar mooi over weg gaan met de commandline. Geen grafische toepassingen voor systeembeheer.
You can never make a system 100% secure unless you unplug the machine from all networks, turn it off, lock it in a safe, smother it in concrete and never use it.