Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2003
    Posts
    23

    Unanswered: 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; 09-22-03 at 16:52.
    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
    Join Date
    Sep 2003
    Posts
    23

    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:ut);
    while (getline(input1,calcstring))
    cout<<calcstring<<endl;
    input1.close();
    input1.open("calc.txt",ios:ut);
    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.

  3. #3
    Join Date
    Sep 2003
    Posts
    23
    I found it. You call this right after the first time you do input.close();

    input1.clear(); // Call clear() to clear the fail() flag before reopening
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •