Rewind a ifstream file

Hi people, i have this code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106



#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include<cmath>
#include"comparison.h"
using namespace std;

void comparison(ifstream *t, ifstream *r);
void reference(ifstream *r, int residues);
char* argv[5];


int main(int argc, char* argv[]){


typedef struct ATOM { string atm; double x; double y; double z;} atom;

int count = 0;

/* Here is the input part, input files must be in XYZ format */

if (argc < 6)
{
	cout <<  "Missing Informations !" << endl << endl << "Input must be : ./eulertrj input_file ref_structure  Natoms Nframes output_file" << endl <<
		endl;
	exit(1);
}

ifstream t(argv[1]); 		// Trajectory File
ifstream r(argv[2]);		// Reference Structure File
ofstream o(argv[5]);		// Output File

int natoms = atoi(argv[3]);
int nframes = atoi(argv[4]);
int rtmp;
int ttmp;
int residues = (natoms-2)/7;

/* Make a comparison between reference structure and input file, number of atoms and atom sequence MUST be the same, function for comparison in included in comparison.h file*/

comparison(&t,&r);

t.seekg(0, ios::beg);       // Rewind trajectory file
r.seekg(0, ios::beg);       // Rewind Reference file

reference(&r,residues);

return 0 ;
}




void reference(ifstream *r, int residues)
	{
int rtmp;
int xht, yht, zht;
int xtmp, ytmp, ztmp;
double x,y;

string ht2;
string c;
string d;

	r[0] >> rtmp >> c >> c >> c;
	cout << rtmp << endl;

/* In order to obtain the same number of atoms in residue, there is the need to skip one terminal hydrogen (HT2) */

	r[0] >> ht2 >> xht >> yht >> zht;


	for(int count = 0; count <= residues; count++)
		{
		r[0] >> c >> rtmp >> rtmp >> rtmp;		/* Read Structure, 7 atoms per residue */
			if(count==0){	cout << endl << "Sequence Structure is: " << c << ", ";}

		r[0] >> c >> rtmp >> rtmp >> rtmp;
			if(count==0){	cout << c << ", ";}

		r[0] >> c >> rtmp >> rtmp >> rtmp;
			if(count==0){	cout << c << ", ";}

		r[0] >> c >> rtmp >> rtmp >> rtmp;
			if(count==0){	cout << c << ", ";}

		r[0] >> c >> rtmp >> rtmp >> rtmp;
			if(count==0){	cout << c << ", ";}

		r[0] >> c >> rtmp >> rtmp >> rtmp;
			if(count==0){	cout << c << ", ";}

		r[0] >> c >> rtmp >> rtmp >> rtmp;
			if(count==0){	cout << c << ", " << endl << endl;}


		}
	}



i am trying to rewind the files called t and r, because funcion comparison get to the end of file...then in reference i need to rewind them, but...if you look to cout << rtmp, which is a integer (actually is 394), the program give 0 in the output...

in the following there is also comparison.h


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include<cmath>

using namespace std;

void comparison(ifstream *t, ifstream *r)
	{
int rtmp;
int ttmp;
double x,y;
int h = 0;
string c;
string d;

	do
	{
		if (h == 0)
		{
			r[0] >> rtmp >> c >> c >> c ;
			t[0] >> ttmp >> d >> d >> d ;

			if(rtmp != ttmp)
			{
				cout << " Number of Atoms mismatch." << endl << endl << "Check Reference Structure and Trajectory File ! " << endl << endl;
				exit(1);
			}
		}
		else
		{
			r[0] >> c >> x >> x >> x ;
			t[0] >> d >> y >> y >> y ;

		if(!(r[0].eof()))
		{		
			if(c != d)
			{	
				cout << "Structures are different. " << endl << endl << "Check Reference Structure and Trajectory File at line " << 						h+2 << "!" << endl << endl;

			cout << "\t" << c << "\t" << x << "\t" << x << "\t" << x << endl << endl;
			cout << "\t" << d << "\t" << y << "\t" << y << "\t" << y << endl << endl;

				exit(1);
			}
		}
		}	
		if (r[0].eof()) break;
		h++;

	}while(true);

	cout << endl <<  "Structure check......OK!" << endl << endl;
	t[0].seekg (0, ios::beg);       // Rewind trajectory file
	r[0].seekg (0, ios::beg);       // Rewind Reference file

}




i can't see the problem...can you help me?

Thanks!!!
Anyone can help me?
> i am trying to rewind the files called t and r, because funcion comparison get to the end of file...

Once you reach end of stream, the stream would be in a failed and eof state. Before you can use it again, the state must be cleared.
1
2
    t[0].clear() ; 
    // etc. 

See: http://cplusplus.com/reference/iostream/ios/clear/
Topic archived. No new replies allowed.