1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

inputing into files

Discussion in 'Software Development' started by calculus87, Sep 22, 2003.

Thread Status:
Not open for further replies.
  1. calculus87

    calculus87 Thread Starter

    Joined:
    Sep 4, 2003
    Messages:
    34
    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
     
  2. calculus87

    calculus87 Thread Starter

    Joined:
    Sep 4, 2003
    Messages:
    34
    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::eek:ut);
    while (getline(input1,calcstring))
    cout<<calcstring<<endl;
    input1.close();
    input1.open("calc.txt",ios::eek:ut);
    while (getline(input1,calcstring))
    cout<<calcstring<<endl;
    input1.close();


    return 0;
    }


    Thanks for the help
    Sincerely,
    Ronald
     
  3. calculus87

    calculus87 Thread Starter

    Joined:
    Sep 4, 2003
    Messages:
    34
    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
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/166618

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice