I am trying to change some data indside a txt file, based on the input given by the user.The input file is indata.txt(The commented part is, what it holds).And the output file is "outdata.txt"...

The problem is with line 52 and 53.Notice that in the loop from line 55 to 60, the pointer position of streams is being changed.So, to compare and write something for next iteration, i need to reset the position of the pointer.Otherwise the loop(55-60) is going to terminate with out doing anything.It will already reach it's end after the first loop.

I have googled a lot and came across huge amount articles where it says that line 52 and 53 should reset the position of the pointer.But, the seekp and seekg methods are not working...the pointer is not being reset to the start...

Can anyone help me to find out why this is not working....

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
using namespace std;

int main()
{
	int routin[6][7][6] = { 0 };	
	int teacherID,classCountOnCourse,  courseID, creditHour, chunk, x, y, z, position, count_i = 0;
	string stream_str;
	stringstream _temp;
	
	//This is what contails indata.txt
	/*{ 
	ofstream testDataStream("indata.txt");
		for(int i = 0; i < 6; i++)
			for(int j = 0; j < 7; j++)		
				for(int k = 0; k < 6 ; k++)
				{
					count_i++;
					if(count_i <= 251)
						testDataStream << i << j << k << " " << routin[i][j][k] << endl;	
					else
						testDataStream << i << j << k << " " << routin[i][j][k];	
				}
	testDataStream.close();
	}*/

	ifstream inStream("indata.txt");
	ofstream outStream("outdata.txt");	

	cout << "Enter Teacher ID - ";
	cin >> teacherID;	
	cout << "Enter class count - ";
	cin >> classCountOnCourse;

	for(int i=0; i<classCountOnCourse; i++)
	{
		cout << endl <<"For class# " << i << endl;		
		cout << "Enter CourseID, Credit Hour, Class Takes - ";
		cin >> courseID >> creditHour >> chunk;

		for(int j = 0; j<chunk; j++)
		{		
				cout << endl << "Enter time slot(" << j << ")- ";
				cin >> x >> y >> z;		
				position = 100*x + 10*y + z;

				_temp << (teacherID * 100 + i);	
				
				inStream.seekg(0, ios::beg);
				outStream.seekp(0, ios::beg);				

				while(getline(inStream, stream_str))
				{					
					if(atoi(stream_str.substr(0,3).c_str()) == position) 
						stream_str.replace(4, 5, _temp.str());					
					outStream << stream_str << endl;
				}		
		}
	}

	outStream.close();
	inStream.close();
	return 0;
}

Recommended Answers

All 2 Replies

Try putting inStream.clear(), outStream.clear() to clear any eofs.

<deleted>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.