inputing into files

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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
 

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
 

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
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Top