Initialization of ifstream and ofstream

I have a code that reads in various texts files one by one to do some operation on them. Files have similar names differing by a number at the end. I define input and output file as

ifstream inpfile;
ofstream outfile;

Now, there is a for loop to take care of multiple files, where index of for loop is used as a number at the end of file name. If I provide above statements within for loop, then program does operation on all files. However, if I define this outside for loop, then operation is done on only very first file. For all other files, starting from second one, it says that it is already at the end of file.

I have provided

inpfile.close();
outfile.close();

commands within for loop, so there is nothing that files are not being closed. All suggestions are appreciated.
Actually,Im totally surprised and I cant find any reason for such trouble!I have done so many things with file manipulation but its the first time I deal with such a problem.The program below concatenates files with names:part_00001,part_00002,part_00003,etc.I want you to PAY ATTENTION mainly to the KERNEL LOOP.As u can see I have provided the statement(ifstream,ofstream) in the beginning of the code without dealing with any trouble.
Last edited on
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include<iostream>
#include<fstream>

using namespace std;

const  int SIZE=4096;//4KB
unsigned long int File_Size(ifstream& ifile);
short int ProgreSS_PerCeNT(unsigned long int i,unsigned long fsize);
int INTEGER_TRANSFORMATION(char system_number[]);

main()
{          
 ifstream in_file;
 ofstream out_file;

 unsigned long int fsize;
 short int count=0;
 char source_driver;
 int c=0;
 short int count1=48,count2=48,count3=48,count4=48,count5=49;
 int partial=0;
 char str_partial[8];
 char destination_file[52]; 

 do
 {
  fflush(stdin);
  cout<<"Give parts:";
  cin.get( str_partial , 7 );
  partial=INTEGER_TRANSFORMATION( str_partial );
 }while( partial==-1 );//--controls if the number of parts is greater that 0

 
 fflush(stdin);
 cout<<"Give the driver of the parts:";
 cin>>source_driver;//--for WindowS only

char source_file[14]={source_driver,':','\\','p','a','r','t','_',count1,count2,count3,count4,count5,'\0'};

 fflush(stdin);// cin.ignore(4096,'\n'); for LinuX
 cout<<"Give the destination file (up to 50 character):";
 cin.get( destination_file , 51 );

 int STEP=1;
 fflush(stdin);
 cout<<"Give progress STEP:";
 cin>>STEP;


 out_file.open(destination_file,ios::binary);
 if( !out_file.is_open() )
 {
  cerr<<"\nERROR with file destination:["<<destination_file<<"]"<<endl;
  for(int i=0; i<5; i++)
  {
   cerr<<"********************************************************"<<endl;
  }
  cerr<<"******************PROGRAM TERMINATED********************\n";     
  exit(1);
 }


 system("cls");//syste("clear"); for LinuX
 cout<<"-------------PLeaSE WaiTE----------------\n"<<endl;

 char buf[SIZE];
 unsigned long int b=SIZE;


 for(int i=0; i<partial; i++)//--KERNEl LOOP
 { 
  source_file[8]=count1;
  source_file[9]=count2;
  source_file[10]=count3;
  source_file[11]=count4;
  source_file[12]=count5;

 
  count5++;
  if( count5==58 )
  {
   count5=48;
   count4++;
   if( count4==58 )
   {
    count4=48;
    count3++;
    if( count3==58 )
    {
     count3=48;
     count2++;
     if( count2==58 )
     {
      count2=48;
      count1++;
     }
    }
   }    
  }

  in_file.open(source_file,ios::binary);
  if( !in_file.is_open() )
  {
   cerr<<"\nERROR with the part:["<<source_file<<"]---probably NOT FOUND"<<endl;
   for(int i=0; i<5; i++)
   {
    cerr<<"********************************************************"<<endl;
   }
   cerr<<"******************PROGRAM TERMINATED********************\n";     
   exit(1);
  }

  fsize=File_Size(in_file);
  cout<<"--copying  ["<<source_file<<"] size:"<<fsize<<"bytes to ["<<destination_file<<"]  progress -- 0% ";

  b=SIZE;
  int k=b;
  for(unsigned long int j=0; j<fsize; j+=b)
  {
   /***********************/
   /**********************/
   if(fsize-j<SIZE)
   {
    b=fsize-j;
   }   
   in_file.read(buf,b);
   out_file.write(buf,b);
   /***********************/
   /***********************/
   short int p=ProgreSS_PerCeNT(j,fsize);
   if( p==count )
   {
    system("cls");
    cout<<"-------------PLeaSE WaiTE----------------\n"<<endl;
    cout<<"--copying  ["<<source_file<<"] size:"<<fsize<<"bytes to ["<<destination_file<<"]  progress --"<<p<<"%";

    count+=STEP;
   }
  }

  in_file.close();
  system("cls");
  cout<<"-------------PLeaSE WaiTE----------------\n"<<endl;

  cout<<"--copying  ["<<source_file<<"] size:"<<fsize<<"bytes to ["<<destination_file<<"]  progress --100% ";

  cout<<" ---***COMPLETED---***"<<endl;
  count=0;
 }//end of for      

 cout<<"\nTOTAL SIZE OF new file=>["<<destination_file<<"]: "<<out_file.tellp()<<"bytes."<<endl; 
 cout<<"count:"<<count<<endl;
 out_file.close();

 cout<<"\n\n------------------program ended successfully----------------------\n"<<endl;

 system("pause");
}//end of main
/////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
unsigned long int File_Size(ifstream& ifile)
{
 ifile.seekg(0,ios::end);
 unsigned long int fsize=ifile.tellg();
 ifile.seekg(0,ios::beg);
 return fsize;
}
//#########################################################
//########################################################
short int ProgreSS_PerCeNT(unsigned long int i,unsigned long fsize)
{
 short int percent=0;
 float p;
  
  if( i>0 )
  {
   p=(float)fsize/(float)i;
   percent=100/p;
   return percent;
  }
}
/****************************************************************************/
/***************************************************************************/
int INTEGER_TRANSFORMATION(char system_number[])
{
 int sum=0;
 for(int i=0; system_number[i]!='\0'; i++)
 {
  if( (i==0 && system_number[i]==48) || system_number[i]<48 || system_number[i]>57 )
  {
   return -1;
  }       
 }
 sum=atoi(system_number);
 return sum;
}


Last edited on
--The program above works only for WindowS OS.If you want it to be compatible to LinuX
you have to change the "source_file[]" array and the "source_driver" along with the LinuX ones.
Thanks for this. First thing, I am new to C++ and this is my first code. So, I'd like you to have a look at it. I have ifstream and ofstream inside for loop and it's working. Try using it with ifstream and ofstream outside for. I need to develop this code to be used on both OSs, linux as well as Windows.

You can generate input file as text files, having .ibl as extension and having begin curve and end curve and few coordinate points in between, like

begin curve
0.03 0.4 0.01
5.2 2.9 0.3
end curve
begin curve
0.3 0.4 2
end curve

Just these few lines will work as input files. You can generate two same files with same data. All I am telling is that it will work for first but not for the second one if ifstream and ofstream are outside for. Currently, code has been written in VC++ 2005.NET
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
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

void main()
{
	int i, nfile;
	char fnm[256], fname[256], new_fname[256];
	char line1[256],line2[256],line3[256];
	//ifstream inpfile;
	//ofstream outfile;
	cout<<"Enter File Name (without number, without extension): "<<endl;
	cin>>fnm;
	cout<<"Number of files to be processed with similar name: "<<endl;
	cin>>nfile;
	for(i=1; i<=nfile; i++)
	{
		ifstream inpfile;
		ofstream outfile;
		sprintf_s(fname,"%s%d.ibl",fnm,i);
		sprintf_s(new_fname,"%s%d_n.ibl",fnm,i);

		inpfile.open(fname);
		outfile.open(new_fname);

		if(!inpfile.is_open())
		{
			cout<<"Error opening Input file. Check that the file exists and path is correct for " << fname;
			return;
		}
		if(!outfile.is_open())
		{
			cout<<"Error opening Output file. Check permissions.\n";
			return;
		}
		//inpfile.seekg(ios::beg);
		if(inpfile.eof()==true)
		{
			cout<<fname;
		}
		while(!inpfile.eof())
		{
			inpfile.getline(line1,255);
			if(strstr(line1,"begin curve")!=NULL)
			{
				inpfile.getline(line2,255);
				inpfile.getline(line3,255);
				if(strstr(line3,"end curve")!=NULL)
				{
					continue;
				}
				else
				{
					outfile<<line1<<endl;
					outfile<<line2<<endl;
					outfile<<line3<<endl;
					continue;
				}
			}
			else
				outfile<<line1<<endl;
		}
		inpfile.close();
		outfile.close();
	}
}
I cant find any reason this program not to work well having those statements out of the loop.!!!My friend im really surprised!!The program works perfect whether the statements are within the loop or out of it!!!!!!
Thanks a lot DimitriuS. Even we're unable to find a plausible reason for it's status when statements are out of loop, so, for the time being we're using it as it is.
Topic archived. No new replies allowed.