class of vector ???

This is a discussion on class of vector ??? within the C++ Programming forums, part of the General Programming Boards category; Hello everybody ! I been trying for hours to figure out how to use countLine in this code. It's a ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    90

    class of vector ???

    Hello everybody !

    I been trying for hours to figure out how to use countLine in this code. It's a vector class and it use the raw pointers technique which is all new to me. For a simple vector I can use this:

    Code:
    cout << v_DATA.size();
    ... but nothing I tried worked in this class of vector. Could someone show me how to get the lineCount (Element Count) to work. Thanks in advance.

    Source found here: c++ - How to write the content of different files to a vector for further use with getline - Stack Overflow


    Code:
    //  ...   You can use a class which handles some raw pointers (For Large Files)
    
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    using namespace std;
    
    
    class file_vector 
    {
    public:
      file_vector()
      {}
    
      virtual ~file_vector()
      {
        for( std::vector< std::ifstream* >::iterator it = 
                        m_file_streams.begin(); it != m_file_streams.begin(); it++)
        {
          std::ifstream * p_stream = *it;
          delete p_stream;
        }
      }
    
      void append_file(const std::string& file_name)
      {
        std::ifstream * p_stream = new std::ifstream( file_name.c_str() );
        if(p_stream->is_open())
          m_file_streams.push_back(p_stream);
        else
          delete p_stream;
      }
    
      void reset()
      {
        for( std::vector< std::ifstream* >::iterator it = 
                          m_file_streams.begin(); it != m_file_streams.end(); it++)
        {
          std::ifstream * p_stream = *it;
          p_stream->seekg(0,p_stream->beg);
        }
      }
    
      size_t size()
      {
        return m_file_streams.size();
      }
    
      std::ifstream & get(size_t index)
      {
        return * m_file_streams.at(index); // Using at because of index check
      }
    
      std::ifstream & operator [] (size_t index)
      {
        return get(index);
      }
    
    private:
      std::vector< std::ifstream* > m_file_streams;
    };
    
    
    int main()
    {
      file_vector files_content;
      files_content.append_file("file1.txt");
      files_content.append_file("file2.txt");
      files_content.append_file("file3.txt");
    
      for(size_t i = 0; i < files_content.size(); i++)
      {
        std::string current_line;
        while(std::getline(files_content[i],current_line))
          std::cout << current_line << std::endl;
      }
    
      files_content.reset(); // To make getline usable again
    
    
     cin.get();
     	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    What is countLine?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    Code:
    //  Make a file name "file1.txt" and add a few lines in it.
    //  Save the file and run this program.  I just call it count lines
    //  C++ people say counts elements.
    //  This set-up give you a full string in one line, so make a few extra line.
    //  It should work, if not let me know.  Thank you
    
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
     vector<string> v_DATA;
     string line;
    
     ifstream in("file1.txt");
    
        while (getline( in, line, '\n' ))
            {
              v_DATA.push_back( line );
            }
                sort( v_DATA.begin(), v_DATA.end() );
                for(int i = 0; i < v_DATA.size(); ++i)
                cout << v_DATA[i] <<  "\n";  
    
     cout << "\n\n";
     cout<<v_DATA.size(); // count Lines - - Show the number of lines in the file.
    
    in.close();
    
     cin.get();
     	return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Sorry, I really don't understand what you are having problems with. You provided your file_vector class with a size() member function, so just use that. I don't see a problem (other than certain potential problems with your file_vector class, e.g., you should disable the copy constructor and copy assignment operator, and it is not clear why you want the class to be a base class when there are no virtual member functions besides the destructor).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    Sorry, I really don't understand what you are having problems with. You provided your file_vector class with a size() member function member function, so just use that
    I am not an expert like some of you may have became overnight and I did not completely understand the pointer/reference thing after completing a single c++ textbook among seven other class textbooks that needed my attention also. My instructors told me it could take years to master C++ or did he lie to me? If I knew how to work inside class vector build on pointers I would not have wrote two pages here asking for help. I'm sure you and everyone else knows that once I find or is shown the point of entry there is not a darn thing I cannot do afterward. Can you show me, in code, how to achieve what I am after? Just one line of code (or my own line posted above) and where to place it. sharris was not build in a day, I mean Rome'.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    Ignoring all the whining: Counting the lines of one file should not be different from counting the lines of N files (even if you store streams in a file_vector class). Counting the lines of many files together implies that you need to loop the code that reads the file and counts said lines. That's it.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    So, where do the loop go? Starting tomorrow I will but right now I don't have the smarts or the strength to step through the code to find that location, but I know for sure if I did I would have my TRUE working bucketSorter inserted before morning. I must of wanted it too bad. After more than ten hours of praying for an direct answer here and fifteen hours of coding and writing this 1000 word thread, i give up. I need sleep. I'll find my own answers. You guys have a nice day.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,536
    In cases like these, you simply need to take a step back and think about the logic.
    Logic should tell us that to count the lines of all files, we should do something like:
    For every file
    Count lines of file
    Repeat

    And how do we count the lines of a single file?
    And please fix the indentation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    Logic, I can't take it. I need a debugger so I can see logic and action, that way I'll know for sure. You guy got debugger in your heads and I am not going out like that ... My dream to talk the talk so I can walk the walk is over ... I rather CODE, learn from examples, and get answers in code and not logic, unless it super simple.

    Today, after I wake up, I will find the tools I need. I just been too excited and in a rush. I can't wait but I'm ok now. This way I don't have to ask questions.

    And please fix the indentation.
    Well at lease I use
    Code:
     cin.get();
     	return 0; :)
    Anyway, it be back in order soon.

    Thanks


    Btw: I always wanted a reason to work with pointer and this file was where I plan to start injecting my code so I can learn more. It was my gift from the other C++ gods
    Last edited by sharris; 04-20-2011 at 04:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making stack class using vector class
    By spank in forum C++ Programming
    Replies: 9
    Last Post: 08-10-2007, 03:50 AM
  2. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  3. Vector class
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2002, 11:02 AM
  4. Vector class
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-21-2002, 08:39 PM
  5. stl vector class
    By ygfperson in forum C++ Programming
    Replies: 3
    Last Post: 04-17-2002, 12:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21