Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

112 Neutral

About GungHo88

  • Rank
  1. GungHo88

    Question about some code

    Thanks. And what is the pre increment doing. Specifically this here: (*m_str) >> m_row Here is the code for the row class #include <iterator> #include <iostream> #include <fstream> #include <sstream> #include <vector> #include <string> class CSVRow { public: std::string const& operator[](std::size_t index) const { return m_data[index]; } std::size_t size() const { return m_data.size(); } void readNextRow(std::istream& str) { std::string line; std::getline(str,line); std::stringstream lineStream(line); std::string cell; m_data.clear(); while(std::getline(lineStream,cell,',')) { m_data.push_back(cell); } } private: std::vector<std::string> m_data; }; std::istream& operator>>(std::istream& str,CSVRow& data) { data.readNextRow(str); return str; } int main() { std::ifstream file("plop.csv"); CSVRow row; while(file >> row) { std::cout << "4th Element(" << row[3] << ")\n"; } }
  2. class CSVIterator { public: typedef std::input_iterator_tag iterator_category; typedef CSVRow value_type; typedef std::size_t difference_type; typedef CSVRow* pointer; typedef CSVRow& reference; CSVIterator(std::istream& str) :m_str(str.good()?&str:NULL) { ++(*this); } CSVIterator() :m_str(NULL) {} // Pre Increment CSVIterator& operator++() {if (m_str) { (*m_str) >> m_row;m_str = m_str->good()?m_str:NULL;}return *this;} // Post increment CSVIterator operator++(int) {CSVIterator tmp(*this);++(*this);return tmp;} CSVRow const& operator*() const {return m_row;} CSVRow const* operator->() const {return &m_row;} bool operator==(CSVIterator const& rhs) {return ((this == &rhs) || ((this->m_str == NULL) && (rhs.m_str == NULL)));} bool operator!=(CSVIterator const& rhs) {return !((*this) == rhs);} private: std::istream* m_str; CSVRow m_row; }; int main() { std::ifstream file("plop.csv"); for(CSVIterator loop(file);loop != CSVIterator();++loop) { std::cout << "4th Element(" << (*loop)[3] << ")\n"; } } What is this line incrementing when it increments *this? CSVIterator operator++(int) {CSVIterator tmp(*this);++(*this);return tmp;}
  3. GungHo88

    ofstream problem with folders

      Can you post a working example. It's still not working. 
  4. GungHo88

    ofstream problem with folders

    I tries \\ and / but now it doesn't open my file. 
  5. Ello. This code is not working as intended. I have my exe and in the same folder I have an inventories folder with text files. This code won't open inventories\Noa.txt. It creates a file called inventoriesNoa.txt in the same folder as my cpp files. I want it to properly navigate to the inventories\Noa.txt file and search in the same directory the exe is in. How do I do this? #include <conio.h> #include <fstream> #include <iostream> using namespace std; int main() { ofstream out; out.open("\inventories\Noa.txt"); if (!out.is_open()) { cout << "The file could not be opened"; } out << "hi"; getch(); return 0; }
  6. Ello. The strings in this vector say null everytime i run it. entity.h #include <vector> #include <string> using namespace std; class Hero { public: vector<string> storage; void addStorageItem(string); }; entity.cpp #include "entity.h" Hero::Hero() { } Hero::~Hero() { } void Hero::addStorageItem(string x) { storage.push_back(x); } main() #include <stdio.h> #include <conio.h> #include "entity.h" int main() { Hero myHero = Hero(); myHero.addStorageItem("Hi"); myHero.addStorageItem("How are you"); for(int i = 0; i < myHero.storage.size(); i++) { printf("item Name: %s\n", myHero.storage[i]); } getch(); return 0; }
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!