'da ifstream'i kullanarak bir metin dosyasını açarken sorun mu yaşıyorsunuz? Bir dosyadan metin alan ve bunu bir Grafik veri yapısına ekleyen bir metin işlemcisi üzerinde çalışıyorum. Grafiği yaptım, ancak metin işlemcisiyle ilgili sorun yaşıyorum. Kodu ne zaman çalıştırsam, dosyayı açamıyorum diyor. Kodu uygularken metin dosyasının aynı dizinde olduğundan emin oldum. Sadece ben aslında Grafik yazılı çalışmadan önce ilk olarak bir dosyadan okumaya çalışıyorumC++
#include <fstream>
#include <cstring>
#include <string>
#include "Graph.h"
class GraphTextProcessor {
private:
Graph* m_data;
public:
GraphTextProcessor();
Graph* process(std::string filename);
};
GraphTextProcessor::GraphTextProcessor() {
}
Graph* GraphTextProcessor::process(std::string filename) {
//process text file and insert into graph here
std::string word;
//opens file in read mode
std::ifstream readFile;
readFile.open(filename.c_str(), std::ios::in);
if (readFile.is_open()) { //Not opening
while (readFile >> word) {
std::cout << word << std::endl;
}
// Closes open text file
readFile.close();
}
else {
std::cout << "Unable to open text file." << std::endl;
}
return NULL;
}
: İşte GraphTextProcessor sınıf için kodudur.
#include <iostream>
#include <string>
#include "GraphTextProcessor.h"
int main() {
GraphTextProcessor *gp = new GraphTextProcessor();
gp->process("hello.txt");
}
O "Unable to open text file"
yazdırır: İşte Main çalıştırıyorum koddur. Baska öneri?
Bu yardımcı olabilir: (http://stackoverflow.com/a/17338934/669576) –
Bunu kullanışlı açmak için senin '" açılamadı olması bulabilir [lfStream açtığınızda hata mesajı almak nasıl başarısız] ... '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ' Programınızı bir IDE'den çalıştırıyorsanız, beklediğiniz yerde olmayabilir. –