Brauche Hilfe bei Projekt



  • Nabend,
    wir haben in unserer Klasse eine Aufgabe über die Ferien aufbekommen, wir müssen ein Verzeichnis in dem eine beliebe Anzahl von .txt Dateien sind öffnen, alle .txt Dateien auslesen und am Ende die Anschlagszahl (von der ich schon den quellcode hab wie man von einer Einzelnen Datei die Anschlagszahl ausließt) alle .txt Dateien in eine .txt Datei reinschreiben.
    Nur das Problem ist das wir das auslesen eines Verzeichnises noch nicht in der Schule gelernt haben und das ich jetzt schon zimlich lange dranhäng und einfach nicht weiterkomm.
    Ich hoffe das ihr mir helfen könnt.

    MfG
    pc-master



  • Hallo,

    welches OS benutzt du?
    Falls Windows: http://www.mpdvc.de/html.htm#Q12
    Falls Linux: http://www.galileo-press.de/openbook/c_von_a_bis_z/c_019_002.htm

    Wenn du's platformunabhängig brauchst, nimm boost::filesystem : http://www.boost.org/libs/filesystem/doc/index.htm

    MfG

    GPC



  • Vielen Dank für die Hilfe

    Ich habe mir des ma angeschaut und auhc versucht nur allerdings komm ich nicht weiter.

    Könntet ihr mir des genauer erkären bzw. einen Beispielscode zeigen?

    MFG pc-master



  • pc-master schrieb:

    Vielen Dank für die Hilfe

    Leider hast du immer noch nicht gesagt, welches OS du benutzt.

    Ich habe mir des ma angeschaut und auhc versucht nur allerdings komm ich nicht weiter.

    Geht's auch ein wenig genauer als "ich komm nicht weiter"? Sicher, dass du nicht nur jemanden suchst, der deine Arbeit macht?

    Könntet ihr mir des genauer erkären bzw. einen Beispielscode zeigen?

    Auf den Seiten, deren Links GPC gepostet hast, sind doch Beispiele, oder? 😕



  • moin,

    ja ich arbeite unter windows.

    Die beiden Beispiele für Windows lassen sich nicht compilieren im Borland, weil immer FEhler kommen.

    MFG pc-master



  • pc-master schrieb:

    Die beiden Beispiele für Windows lassen sich nicht compilieren im Borland, weil immer FEhler kommen.

    Und du möchtest jetzt was von uns hören... 😕



  • Hier eigene Beispielimplementierung die unter debian und windoof funzen. Die Schnittstelle entspricht weitgehend der File Klasse in Java. Einige Methoden sind noch zu implementieren, bzw zu verbessern. Falls jemand bock hat, kann er ja was dazu sagen.

    Header sio.h

    #ifndef CFILE_H_
    #define CFILE_H_
    
    #if defined(__unix__)
    	#include <dirent.h>
    	#include <unistd.h>
    
    	#define MODUS 0711
    
    	#define EXISTS 	F_OK
    	#define EXEC	X_OK
    	#define WRITE	W_OK
    	#define READ	R_OK
    
    #elif defined(WIN32)
    	#include <direct.h>
    	#include <windows.h>
    	#include <io.h>
    
    	#define EXISTS  S_IREAD
    	#define EXEC	S_IEXEC
    	#define WRITE	S_IWRITE
    	#define READ	S_IREAD
    
    #endif
    
    #include <sys/stat.h>
    #include <string>
    #include <vector>
    
    class CFile {
    private:
    	std::string m_strFileName;
    	std::string m_strParentFileName;
    
    public:
    
    	CFile(std::string strFileName, std::string strParentFileName = "");
    	CFile(const CFile& rclFile);
    	~CFile();
    
    	CFile& operator=(const CFile& rclFile);
    
    	bool operator==(const CFile& rclFile) const;
    	bool operator!=(const CFile& rclFile) const;
    
    	bool IsFile() const;
    	bool IsDirectory() const;
    	bool IsHidden() const;
    
    	long lastModified() const;
    
    	bool RenameTo(const CFile& rclFile);
    	bool SetReadOnly() const;
    
    	std::vector<std::string> ListFiles() const;
    	std::string GetName() const;
    	std::string GetParent() const;
    	std::string GetPath() const;
    
    	CFile GetParentFile() const;
    
    	bool Exists() const;
    	bool Delete() const;
    	bool CreateNewFile() const;
    	bool CreateNewDir() const;
    };
    
    #endif /*CFILE_H_*/
    

    Implementierung sio.cpp

    #ifdef WIN32
    // Disable visual c++ warning C4786 
    //"... identifier was truncated to '255' character in the debug information
    #pragma warning (disable: 4786)
    #endif
    
    #include "sio.h"
    
    CFile::CFile(std::string strFileName, std::string strParentFileName) {
    
    	m_strFileName = strFileName;
    	if(strParentFileName == "") {
    		char buffer[256];
    		if(getcwd(buffer, sizeof(buffer)) != NULL)
    			strParentFileName = std::string(buffer);
    	}
    	m_strParentFileName = strParentFileName;
    }
    
    CFile::CFile(const CFile& rclFile) {
    	m_strParentFileName = rclFile.m_strParentFileName;
    	m_strFileName = rclFile.m_strFileName;
    }
    
    CFile::~CFile() {
    
    }
    
    CFile& CFile::operator=(const CFile& rclFile) {
    	if(this != &rclFile) {
    		m_strParentFileName = rclFile.m_strParentFileName;
    		m_strFileName = rclFile.m_strFileName;
    	}
    	return *this;
    }
    
    bool CFile::IsFile() const {
    
    	if(!Exists())
    		return false;
    
    	bool bIsFile = false;
    
    	struct stat file_status;
    	if(stat(m_strFileName.c_str(), &file_status) == 0) {
    		#if defined(__unix__)
    		if(S_ISREG(file_status.st_mode)  || S_ISCHR(file_status.st_mode))
    		#elif defined(WIN32)
    		if(file_status.st_mode & S_IFREG || file_status.st_mode & S_IFCHR)
    		#endif
    			bIsFile = true;
    	}
    	return bIsFile;
    }
    
    bool CFile::IsDirectory() const {
    
    	if(!Exists())
    		return false;
    
    	bool bIsDir = false;
    
    	struct stat file_status;
    	if(stat(m_strFileName.c_str(), &file_status) == 0) {
    		#if defined(__unix__)
    		if(S_ISDIR(file_status.st_mode)) 
    		#elif defined(WIN32)
    		if(file_status.st_mode & S_IFDIR)
    		#endif
    			bIsDir = true;
    	}
    
    	return bIsDir;
    }
    
    bool CFile::IsHidden() const {
    	// TODO Implement it
    	return false;
    }
    
    bool CFile::operator==(const CFile& rclFile) const {
    	// TODO Implement it
    	return false;
    }
    
    bool CFile::operator!=(const CFile& rclFile) const {
    	// TODO Implement it
    	return false;
    }
    
    std::vector<std::string> CFile::ListFiles() const {
    	std::vector<std::string> vecList;
    
    	if(!IsDirectory() || !Exists())
    		return vecList;
    
    	#if defined(__unix__)
    	DIR* dir = opendir(m_strFileName.c_str());
    
    	struct dirent* entry;
    	while((entry = readdir(dir)) != NULL)
    		vecList.push_back(std::string(entry->d_name));
    	closedir(dir);
    
    	#elif defined(WIN32)
    
    	WIN32_FIND_DATA dir;
    	HANDLE fhandle;
    	char directory[512];
    	sprintf(directory, "%s\\*.*", m_strFileName.c_str());
    
    	if((fhandle = FindFirstFile(directory, &dir)) != INVALID_HANDLE_VALUE) {
    		do {
    			vecList.push_back(std::string(dir.cFileName));
    		} while(FindNextFile(fhandle, &dir));
    	}
    	FindClose(fhandle);
    
    	#endif
    
    	return vecList;
    }
    
    std::string CFile::GetName() const {
    	return m_strFileName;
    }
    
    std::string CFile::GetParent() const {
    	return m_strParentFileName;
    }
    
    CFile CFile::GetParentFile() const {
    	return CFile(m_strParentFileName);
    }
    
    bool CFile::Exists() const {
    	if(access(m_strFileName.c_str(), EXISTS) == -1)
    		return false;
    	return true;
    }
    
    bool CFile::Delete() const {
    
    	if(!Exists())
    		return false;
    
    	if(access(m_strFileName.c_str(), WRITE | READ) == -1)
    		return false;
    
    	if(rmdir(m_strFileName.c_str()) == -1)
    		return false;
    
    	return true;
    }
    
    bool CFile::CreateNewFile() const {
    	if(!Exists()) {
    		FILE* file = fopen(m_strFileName.c_str(), "w+");
    		if(file == 0)
    			return false;
    		return true;
    	}
    	return false;
    }
    
    bool CFile::CreateNewDir() const {
    	if(!Exists()) {
    
    		#if defined(__unix__)
    		if(mkdir(m_strFileName.c_str(), MODUS) != -1)
    		#elif defined(WIN32)
    		if(mkdir(m_strFileName.c_str()) != -1)
    		#endif
    			return true;
    	}
    	return false;
    }
    

    Viel Spass

    Grüße
    Dimitrij


Anmelden zum Antworten