tchembado
Gebruiker
- Lid geworden
- 28 nov 2006
- Berichten
- 93
hoi, ik probeer een programma te maken om te tellen hoeveel txt bestanden ik heb van de vorm mapn.txt
hiervoor heb ik de volgende code.
ik krijg echter altijd '0' als output. Dit komt denkik doordat
"Note that since the returned pointer is of type const, the character data that c_str() returns cannot be modified. Furthermore, you do not need to call free() or delete on this pointer."
want als ik dit slechts 1 keer doe, met bijvoorbeeld n = 4 dan krijg ik aantal = 1. (want het bestand test4.txt) bestaat inderdaad.
Hoe moet ik dit free() of delete gedoe laten werken want dat werkt niet.
Of is er misschien iets anders dat ik fout doe,
alle hulp is welkom,
groetjes,
Tchembado
ps, het convert.h ziet er zo uit:
hiervoor heb ik de volgende code.
Code:
#include "iostream"
#include "fstream"
#include "convert.h"
using namespace std;
ifstream myfile;
int main()
{
int aantal = 0;
string s;
string t = ".txt";
for(int n = 1;n<=20;n++)
{
s = "test";
s+=convert<string>(n);
s+=t;
cout<<s<<endl;
myfile.open(s.c_str());
if (myfile!=0) aantal++;
myfile.close();
}
cout<<"aantal bestaande .txt documenten = "<<aantal;
getchar();
}
ik krijg echter altijd '0' als output. Dit komt denkik doordat
"Note that since the returned pointer is of type const, the character data that c_str() returns cannot be modified. Furthermore, you do not need to call free() or delete on this pointer."
want als ik dit slechts 1 keer doe, met bijvoorbeeld n = 4 dan krijg ik aantal = 1. (want het bestand test4.txt) bestaat inderdaad.
Hoe moet ik dit free() of delete gedoe laten werken want dat werkt niet.
Of is er misschien iets anders dat ik fout doe,
alle hulp is welkom,
groetjes,
Tchembado
ps, het convert.h ziet er zo uit:
Code:
#ifndef CONVERSIE_H
#define CONVERSIE_H
#include <iostream>
#include <sstream>
using namespace std;
template <class doelType, class inputType>
doelType convert(const inputType & t)
{
stringstream s; //een stringstream aanmaken voor zowel lezen als schrijven
s << t; //de input in de stringstream plaatsen
doelType resultaat; //een variable van het juiste output-type aanmaken
s >> resultaat; //de opgeslagen input, naar de output converteren
return resultaat;
}
#endif //CONVERSIE_H
Laatst bewerkt: