io streams problem

I have serious problem actually i want to read many txt files so creating many file streams i want to use just one file stream and I did in pseudo code

while (more file to read)
{
open one file
read data and
close
}

it can read very well from 1st test file but after subsequent files it is not able to read data .So do i have to create every time new file streams for different text files because that way it is working file.

here is actual 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
void FeatureExtractor::AverageStdCalculation() 
	{
	CString filename1=CString(filePath)+"\\"+new_dir_name[0]+"\\"+"userinfo.txt";
	int a ,i ;
	ifstream fp,fp2;
	char username[20];
	char imageno[2];
	int featurevector[5]={0} ;
	int	avgfeature[5] ={0};
	double feature6=0,avgfeature6=0;
	fp.open( filename1, ios_base::out  );
	fp >>username>>a ;
	fp.close();
	for(i=1;i<a;i++)
		{
			_itoa_s(i,imageno,2,10);
			 filename1=CString(filePath)+"\\"+new_dir_name[0]+"\\"+"globalfeature_"+CString(imageno)+".txt";
				fp.open( filename1, ios_base::out  );
				fp.seekg (0, ios::beg);

			while(fp)
				{
				fp>>featurevector[0]>>featurevector[1]>>featurevector[2]>>featurevector[3]>>featurevector[4];
				fp>>feature6;
				}
			avgfeature[0]=featurevector[0]+avgfeature[0];
			avgfeature[1]=featurevector[1]+avgfeature[1];
			avgfeature[2]=featurevector[2]+avgfeature[2];
			avgfeature[3]=featurevector[3]+avgfeature[3];
			avgfeature[4]=featurevector[4]+avgfeature[4];
			avgfeature6=avgfeature6+feature6;
			fp.close();
		
		
		}
	ofstream tfile;
	filename1=CString(filePath)+"\\"+new_dir_name[0]+"\\"+"AverageFeatureVector.txt";
	tfile.open( filename1, ios_base::in |ios::app   );
	tfile<<avgfeature[0]<<'\t'<<avgfeature[1]<<'\t'<<avgfeature[2]<<'\t'<<avgfeature[3]<<'\t'<<avgfeature[4]<<'\t'<<avgfeature6;
	tfile.close(); 

	}
Last edited on
In your for loop, I do not see how you will be attempting to open a new file each time. The line containing fp.open(...) appears to be opening the same file over and over. Is that what you are trying to do?

Also, please edit your post and use code tags. You'll notice a "#" button. Highlight the code and select this button so that your code will appear more readable. It is very difficult to read, especially without any indenting.

Lastly, what you are doing is very dependent on the contents of the file. Have you debugged the program and verified that the first loop works perfectly? What specifically happens after you open the file in the second loop iteration?
even this simple is not working with same filestreampointer

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
CString filename1=CString(filePath)+"\\"+new_dir_name[0]+"\\"+"userinfo.txt";
	int a ,i ;
	ifstream fp,fp2;
	char username[20];
	char imageno[2];
	int featurevector[5]={0} ;
	int	avgfeature[5] ={0};
	double feature6=0,avgfeature6=0;
	fp.open( filename1, ios_base::in  );
	fp >>username>>a ;
	fp.close();
	
			_itoa_s(1,imageno,2,10);
			 filename1=CString(filePath)+"\\"+new_dir_name[0]+"\\"+"globalfeature_1.txt";
				fp.open( filename1, ios_base::in  );
				//fp.seekg (0, ios::beg);

				  cout << fp.rdbuf( )->is_open( ) << endl;

				fp >>featurevector[0]>>featurevector[1]>>featurevector[2]>>featurevector[3]>>featurevector[4];
				fp >>feature6;
				
			avgfeature[0]=featurevector[0]+avgfeature[0];
			avgfeature[1]=featurevector[1]+avgfeature[1];
			avgfeature[2]=featurevector[2]+avgfeature[2];
			avgfeature[3]=featurevector[3]+avgfeature[3];
			avgfeature[4]=featurevector[4]+avgfeature[4];
			avgfeature6=avgfeature6+feature6;
			fp.close();
		
	
	ofstream tfile;
	filename1=CString(filePath)+"\\"+new_dir_name[0]+"\\"+"AverageFeatureVector.txt";
	tfile.open( filename1, ios_base::out |ios::app   );
	tfile<<avgfeature[0]<<'\t'<<avgfeature[1]<<'\t'<<avgfeature[2]<<'\t'<<avgfeature[3]<<'\t'<<avgfeature[4]<<'\t'<<avgfeature6;
	tfile.close(); 

Topic archived. No new replies allowed.