Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read file problem, do not read at the second time 1

Status
Not open for further replies.

SwingXH

Technical User
Jun 15, 2004
97
0
0
US
Hi, everyone,
I got a problem in the C++ programming when reading data file. The codes are attached below. In the for loop, the data file can only be opened once. When j=2, it does not open the file. I don't understand why. Is there anyone can give me a hand? I've been struggling for a while.
Thanks a lot!

SwingXH

#include<iostream>
#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<cmath>
#include<string>
#include <windows.h>
#include <winuser.h>
#include <cstdlib>
using namespace std;

double X;
int count2, count1;

int main()
{
ifstream in1;
ifstream in2;

int j;
for (j=1;j<3;j++)
{
in1.open("x.rez");
count2=0;
while (in1>>X)
{
count2=count2+1; cout<<count2<<endl;
cout<<X<<endl;
}
in1.close();
cout<<count2<<count1<<endl;
}
return 0;
}
 
I actually had a similar problem the other day. Even though you closed an reopened it, it still thinks it's at the end of the file. Just call in1.clear() after you close and it should work fine.
 
Instead of closing and reopening the file, can't you set the file data pointer back to the start of the file. I know you can - I just don't recall the correct file function to use.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top