Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem with ifstream

problem with ifstream

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yonggoo
    wrote on last edited by
    #1

    Hi All, I always appreciate your help. I have a problem with ifstream This is my 'input.txt' file. The first digit of each line is the length. How can I read in this file using while() or for() 2 3 4 5 7 8 10 11 12 7 8 10 16 17 54 37 38 3 33 51 48 ... I tried this one. ... ifstream inpf_EX("input.txt"); for (i=1; i<=N_Of_LINE;i++) { inpf_EX>>EXIST[i][0]); len=EXIST[i][0]; for(j=1; j<=len; j++) { inpf_EX>>EXIST[i][j]); } } Thanks! ...

    Yonggoo

    D 1 Reply Last reply
    0
    • Y Yonggoo

      Hi All, I always appreciate your help. I have a problem with ifstream This is my 'input.txt' file. The first digit of each line is the length. How can I read in this file using while() or for() 2 3 4 5 7 8 10 11 12 7 8 10 16 17 54 37 38 3 33 51 48 ... I tried this one. ... ifstream inpf_EX("input.txt"); for (i=1; i<=N_Of_LINE;i++) { inpf_EX>>EXIST[i][0]); len=EXIST[i][0]; for(j=1; j<=len; j++) { inpf_EX>>EXIST[i][j]); } } Thanks! ...

      Yonggoo

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Yonggoo wrote:

      How can I read in this file using while() or for()

      Neither are necessary, and their use would only serve to confuse you further. You can use an iterator on a stringstream object, and then use the copy() function. You don't need an extra digit in the front to tell how many items follow. If it has to stay, however, just read it in and ignore it.


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      Y 1 Reply Last reply
      0
      • D David Crow

        Yonggoo wrote:

        How can I read in this file using while() or for()

        Neither are necessary, and their use would only serve to confuse you further. You can use an iterator on a stringstream object, and then use the copy() function. You don't need an extra digit in the front to tell how many items follow. If it has to stay, however, just read it in and ignore it.


        "A good athlete is the result of a good and worthy opponent." - David Crow

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        Y Offline
        Y Offline
        Yonggoo
        wrote on last edited by
        #3

        I have not used an iterator on a stringstream. Does std c++ have iterator? Thanks!

        Yonggoo

        D 1 Reply Last reply
        0
        • Y Yonggoo

          I have not used an iterator on a stringstream. Does std c++ have iterator? Thanks!

          Yonggoo

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Yonggoo wrote:

          Does std c++ have iterator?

          Yes.

          class LineItem
          {
          public:
          LineItem() {}
          ~LineItem() {}

          void setLength(const int length) { \_Length = length; }
          int getLength() const { return \_Length; }
          
          void setNumbers(const std::vector<int>& numbers) { \_Numbers.assign(numbers.begin(), numbers.end()); }
          std::vector<int> getNumbers() const { return \_Numbers; }
          

          private:
          int _Length;
          std::vector<int> _Numbers;
          };

          std::ostream& operator<<(std::ostream& os, const LineItem& s)
          {
          os << s.getLength() << "|";

          const std::vector<int> numbers = s.getNumbers();
          std::copy(numbers.begin(), numbers.end(), std::ostream\_iterator<int>(os, " "));
          
          os << std::endl;
          
          return os;
          

          }

          std::istream& operator>>(std::istream& is, LineItem& s)
          {
          int length;
          is >> length;

          std::string sNumbers = "";
          std::getline(is, sNumbers);
          
          std::stringstream ss(sNumbers);
          
          std::vector<int> numbers;
          std::copy(std::istream\_iterator<int>(ss), std::istream\_iterator<<int>(), std::back\_inserter(numbers));
          
          s.setLength(length);
          s.setNumbers(numbers);
          
          return is;
          

          }

          void main( void )
          {
          std::ifstream fin;
          std::vector<LineItem> lines;

          fin.open("c:\\\\input.txt");
          std::copy(std::istream\_iterator<LineItem>(fin), std::istream\_iterator<LineItem>(), std::back\_inserter(lines));
          fin.close();
          
          std::copy(lines.begin(), lines.end(), std::ostream\_iterator<LineItem>(std::cout, "\\n"));
          

          }


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups