#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    60
    Rep Power
    11

    files


    Hello,
    okay here is what I am trying to do.
    in my program I open up a file and use the while(getline(input,word))
    loop go through the text file. After I have gone through the entire file I close it. Now, I would like to go through the file again in the same function, but when I attempt to use the loop again, I am not able to. Now I am not sure why, I am guessing the reason is because I am now at the end of the file, but I am not certain.

    I know there is a command that says that I open a file and start at the end. Is there possible a command that makes me start at the beginning?

    std::ios::ate

    std::ios::???


    Thanks,
    Ronald
    Last edited by calculus87; September 22nd, 2003 at 02:52 PM.
    This message is a natural product made from recycled electrons. The slight
    variations in spelling and grammar enhance its individual character and
    beauty and in no way are to be considered flaws or defects.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    There are a few ways to handle this:
    1. close() your stream object and open() it again. This will position the read pointer at the top again.
    2. Use the seekg() method to reposition your read pointer to the top. See http://www.cplusplus.com/ref/iostrea...eam/seekg.html for an example.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    60
    Rep Power
    11

    some code


    Here is some example code.
    The file contains
    Cat
    Dog

    that is it. Now when I execute it. It will print out:
    Cat
    Dog

    and that is it. It should print go:
    Cat
    Dog
    Cat
    Dog

    any thoughts?

    #include <iostream>
    #include <stdlib.h>
    #include <fstream>
    using namespace std;
    #include <string>
    #include "time.h"
    #include <vector>
    #include <algorithm>
    #include <sstream>

    int main()
    {
    string calcstring;
    ifstream input1;
    input1.open("calc.txt",ios::out);
    while (getline(input1,calcstring))
    cout<<calcstring<<endl;
    input1.close();
    input1.open("calc.txt",ios::out);
    while (getline(input1,calcstring))
    cout<<calcstring<<endl;
    input1.close();


    return 0;
    }


    Thanks for the help
    Sincerely,
    Ronald
    This message is a natural product made from recycled electrons. The slight
    variations in spelling and grammar enhance its individual character and
    beauty and in no way are to be considered flaws or defects.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,592
    Rep Power
    4207
    Looks like the fail() flag is being set when you hit EOF. Oddly enough, the fail bit is not getting cleared when you close() the fstream object. If you use the clear() method before reopening, it works fine. Also, there's no sense in using ios::out for an ifstream --- it'll just ignore that bit. Either use ios::in or nothing at all (since ios::in is implied for an ifstream object).
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    int main(void) {
      string calcstring;
      ifstream input1;
    
      input1.open("calc.txt");
      while (getline(input1, calcstring))
        cout << calcstring << endl;
      input1.close();
    
      input1.clear(); // Call clear() to clear the fail() flag before reopening
      input1.open("calc.txt");
      while (getline(input1, calcstring))
        cout << calcstring << endl;
      input1.close();
    
      return 0;
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    60
    Rep Power
    11
    yep, that did it. Thanks alot. I appreciate the help.

IMN logo majestic logo threadwatch logo seochat tools logo