Kleines Pointer Problem

Der Wolf

Erfahrenes Mitglied
Hallo Freunde,

ich habe ein kleines Problem bei einer Initialisierungsmethode von mir aus dem ich gerade nicht schlau werde. Die Methode sieht folgendermaßen aus:

C:
/**
 * Asks the user where to find the input file.
 * 
 * @param stream		The stream to read the input file.
 * @return					The path to the input file.
 */
std::string getInputStream(std::fstream *stream) {

	std::string path = "";
	while (path == "" || stream == NULL) {
		std::cout << "Enter path to input file: ";
		std::cin >> path;
		stream = new std::fstream();
		stream->open(path.c_str(), std::fstream::in);
		if (!stream->good()) {
			std::cout << "Could not open file " << path << std::endl;
			stream->close();
			stream = NULL;
			continue;
		}
	}

	if (stream == NULL)
		std::cout << "Is null";

	return path;

}

und die Stelle an der ich die Methode aufrufe wie folgt:

C:
	// Get input stream.
	std::fstream *inputStream = NULL;
	std::string inputPath = getInputStream(inputStream);
	if (inputStream == NULL)
		std::cout << "HALLO" << std::endl;

Mein Problem ist nun, dass wenn ich den Pfad zur Datei angegeben habe (die Datei existiert auch), ich trotzdem noch die Ausgabe "HALLO" in der Konsole bekomme. In der Methode selber ist der Stream aber nicht null. Woran liegt das?

LG
Der Wolf
 
Hi.

Natürlich ändert sich der Wert der Variablen nicht durch den Funktionsaufruf. Du übergibst ja auch nicht die Variable, sondern nur den Wert der Variablen.

C++:
std::string getInputStream(std::fstream* & stream) ;
Gruß

\edit: Du hast dort übrigens ein Speicherleck.
 
Zurück