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. Other Discussions
  3. Clever Code
  4. C++: Reading an ifstream

C++: Reading an ifstream

Scheduled Pinned Locked Moved Clever Code
c++graphicsjsonhelpquestion
4 Posts 3 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.
  • P Offline
    P Offline
    PJ Arends
    wrote on last edited by
    #1

    This bit of code has been running flawlessly for quite a while. The data file is a text file that contains a bunch of white space separated integers that get read into a vector. I allow comments in the data file by preceding the comment with a semicolon. I always had revision history comments at the end of the file but yesterday I added a new one at the end and the whole app stopped working. Can you see the problem?

    std::vector<int> targets;
    std::ifstream targetfile(FileName.c_str());
    if (targetfile.is_open())
    {
    int number = 0;
    targets.push_back(number); // place holder for position zero
    while (!targetfile.eof())
    {
    char next = targetfile.peek();
    if (next == ';')
    {
    // bypass the rest of the line as it is a comment
    while (next != '\n')
    {
    targetfile.get(next);
    }
    }
    else if (isspace(next))
    {
    // advance pass the white-space
    targetfile.get(next);
    }
    else
    {
    // grab the number and add it to the vector
    targetfile >> number;
    targets.push_back(number);
    }
    }
    targetfile.close();
    }


    You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

    D W 2 Replies Last reply
    0
    • P PJ Arends

      This bit of code has been running flawlessly for quite a while. The data file is a text file that contains a bunch of white space separated integers that get read into a vector. I allow comments in the data file by preceding the comment with a semicolon. I always had revision history comments at the end of the file but yesterday I added a new one at the end and the whole app stopped working. Can you see the problem?

      std::vector<int> targets;
      std::ifstream targetfile(FileName.c_str());
      if (targetfile.is_open())
      {
      int number = 0;
      targets.push_back(number); // place holder for position zero
      while (!targetfile.eof())
      {
      char next = targetfile.peek();
      if (next == ';')
      {
      // bypass the rest of the line as it is a comment
      while (next != '\n')
      {
      targetfile.get(next);
      }
      }
      else if (isspace(next))
      {
      // advance pass the white-space
      targetfile.get(next);
      }
      else
      {
      // grab the number and add it to the vector
      targetfile >> number;
      targets.push_back(number);
      }
      }
      targetfile.close();
      }


      You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

      D Offline
      D Offline
      Dan Neely
      wrote on last edited by
      #2

      Your final line didn't end with a newline character?

      Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

      P 1 Reply Last reply
      0
      • P PJ Arends

        This bit of code has been running flawlessly for quite a while. The data file is a text file that contains a bunch of white space separated integers that get read into a vector. I allow comments in the data file by preceding the comment with a semicolon. I always had revision history comments at the end of the file but yesterday I added a new one at the end and the whole app stopped working. Can you see the problem?

        std::vector<int> targets;
        std::ifstream targetfile(FileName.c_str());
        if (targetfile.is_open())
        {
        int number = 0;
        targets.push_back(number); // place holder for position zero
        while (!targetfile.eof())
        {
        char next = targetfile.peek();
        if (next == ';')
        {
        // bypass the rest of the line as it is a comment
        while (next != '\n')
        {
        targetfile.get(next);
        }
        }
        else if (isspace(next))
        {
        // advance pass the white-space
        targetfile.get(next);
        }
        else
        {
        // grab the number and add it to the vector
        targetfile >> number;
        targets.push_back(number);
        }
        }
        targetfile.close();
        }


        You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

        W Offline
        W Offline
        WilliamSauron
        wrote on last edited by
        #3

        A newline at the end of a comment is worth two in the bush.

        -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

        1 Reply Last reply
        0
        • D Dan Neely

          Your final line didn't end with a newline character?

          Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

          P Offline
          P Offline
          PJ Arends
          wrote on last edited by
          #4

          Yeah, that was it. The solution was to check for the end of file while attempting to bypass the comment line.

          // bypass the rest of the line as it is a comment
          while (next != '\n' && !targetfile.eof())
          {

          Lesson: always check for EOF when reading a file in a loop.:sigh:


          You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

          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