I am trying to grab some data after opening a text file with append flag.

I am using the following code...but nothing is being printed and program execution is progressing forward.

Can anyone point me my mistake...

fstream outStream("test.txt", ios::app);string testID;


	cout << "Enter Teacher ID - ";
	cin >> teacherID;	

	outStream.seekg(-10, ios::cur);
	getline(outStream, testID);
	cout << testID;

text.txt content :
001 10101
005 10102
002 10103

you may also have to specify the open mode, e.g. filestr.open ("test.txt", fstream::in | fstream::out | fstream::app); With ios::app the file stream is set to end-of-file on each write operation. After the write its position is undefined, especially in text files. If you want to back up 10 characters then you may also have to take into account the line terminating sequence that your operating system uses. MS-Windows is two bytes, while *nix and MAC are one byte. Other operating systems may be something else.

you may also have to specify the open mode, e.g. filestr.open ("test.txt", fstream::in | fstream::out | fstream::app); With ios::app the file stream is set to end-of-file on each write operation. After the write its position is undefined, especially in text files. If you want to back up 10 characters then you may also have to take into account the line terminating sequence that your operating system uses. MS-Windows is two bytes, while *nix and MAC are one byte. Other operating systems may be something else.

Thanks for reply.
But I am trying to grab some data before any write operation.If i consider there is 2 character after each end of line + including 9 character per line, then to take the last line in the text file, we have to write,

outStream.seekg(-11, ios::cur)

But, the problem is simple nothing is being printed. :(

moreover, when i debug this program then i find that the

getline(outStream, testID);

holds nothing (testID = "")

why would it be??

I'm not entirely clear on what you need done. If you want the last line you should probably use ios::end instead of ios::cur

commented: You noticed what I didn't. :) +1

I'm not entirely clear on what you need done. If you want the last line you should probably use ios::end instead of ios::cur

i have already opened the file with ios::app.So the get and set pointer is already at the end of the file.

*I want to open that file in append mode and then i want to print the last line of that file.

i have already opened the file with ios::app.So the get and set pointer is already at the end of the file.

Didn't put 2 and 2 together, apologies

*I want to open that file in append mode and then i want to print the last line of that file.

I understand the immediate task at hand. I'm trying to get a handle on the big picture. I meant that I don't understand the constraints that are preventing you from opening the file without the append and seek back from the end (since maybe there is another way).

Didn't put 2 and 2 together, apologies

I understand the immediate task at hand. I'm trying to get a handle on the big picture. I meant that I don't understand the constraints that are preventing you from opening the file without the append and seek back from the end (since maybe there is another way).

The main task of the program is to append more information to that file.But, I need to check if the current user holds in holds the priority for appending more data.
For example, ID 102 wont be allowed to append anything unless 101 has done it's part.

002 10103

this bold part is the ID.When new user will enter his ID i will check whether it is smaller then this one(101).

That's why i need to open the file in append mode then, i want SubString that last line to compare with current User's input.

Now, why my code cann't simply catch the last line.The intermediate string is containing nothing.(refer to that first and third post)

I've done some experiments with tellg to get the file position after the seekg and it keeps coming back with -1 all of the time. I recalled something about seekg being problematic with text files. I found http://msdn.microsoft.com/en-us/library/y2d6fx99.aspx (scroll down to the yellow box) saying that the offset/seekdirection form shouldn't be used with text files. I'm no expert in this area so I couldn't give you the final work on how true that is.

I would say (and this definitely is not efficient) but open the text file with ios::in, read line by line until the last entry,do your checks,close the file and open it back up again for output (and with append).

002 10103

Is the information stored in separate lines or in continuation?

Is the information stored in separate lines or in continuation?

Separate line.
:)

Try opening file like this:

fstream outStream("test.txt", ios::in|ios::out)

Try opening file like this:

fstream outStream("test.txt", ios::in|ios::out)

That gives better numbers from the seekg function but I think the OP needs to have the append setting on. I suppose the position could be reset after the read is made.

This fares no better in returning the proper string using his original -11.

I think I'll bow out of this one.

Try opening file like this:

fstream outStream("test.txt", ios::in|ios::out)

aren't they implicit flag?

I have solved the problem, though it bugs me whether it will be able to stand in all the situations.

I have explicitly positioned all the pointers.I dont know why,the position of those line is causing problem.For example, if u place seekp line after the teacherID Input the modification doesn't affect the text.txt file.
But if i place that line like this(see in the code), it works perfectly.

Can anyone explain this weird situation?

SOLVED PROBLEMATIC PART:

fstream outStream("test.txt");	
	

	cout << "Enter Teacher ID - ";
	cin >> teacherID;	

	outStream.seekg(-11, ios::end);	
	getline(outStream, testID);		
	if(atoi(testID.substr(4,3).c_str()) >= teacherID){
		cout << "Low priority..." << endl;
		return 0;
	}

	outStream.seekp(0, ios::end);

FULL CODE:

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

int main()
{
	int routine[6][7][6] = {0};

	////////////////////////////////////////////////////////////////////////
	{
	string str;
	ifstream inStream("test.txt");
	
	while(getline(inStream, str))	
		routine[str[0] - 48][str[1] - 48][str[2] - 48] = atoi(str.substr(4,5).c_str());			


	for(int i = 0; i <6 ; i++ )
	{
		for(int j = 0; j < 6; j++)
		{
			for(int k = 0; k< 7; k++)
			{
				if(k <= 6)
				cout  << routine[j][k][i] << "	";
				else
				cout  << routine[j][k][i] << "	" << endl;
			}
			cout << endl << endl;
		}
		cout << endl << endl;
	}
	}
	////////////////////////////////////////////////////////////////////////

	int teacherID,classCountOnCourse,  courseID, creditHour, chunk, x, y, z, position, rValue;
	string testID;
	fstream outStream("test.txt");	
	

	cout << "Enter Teacher ID - ";
	cin >> teacherID;	

	outStream.seekg(-11, ios::end);	
	getline(outStream, testID);		
	if(atoi(testID.substr(4,3).c_str()) >= teacherID){
		cout << "Low priority..." << endl;
		return 0;
	}

	outStream.seekp(0, ios::end);
	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;						
		rValue = teacherID * 100 + i;
		
		if(routine[x][y][z] == 0)
		{
			outStream.fill('0');
			outStream.width(3);
			outStream << position << " " << rValue << endl;
			cout << "Sucessful Entry";
		}
		else
		{
			j--;
			cout << "ERROR : Time slot is occupied by " << routine[x][y][z] << endl;
			//FUNCTION: showing tacher name course name
		}
		// ignoring the previous /n is not creating any problem SO FAR
		}
	}
	outStream.close();

	return 0;
}

I have explicitly positioned all the pointers.I dont know why,the position of those line is causing problem.For example, if u place seekp line after the teacherID Input the modification doesn't affect the text.txt file.
But if i place that line like this(see in the code), it works perfectly.

Which part. I apologize I missed that you were using seekg() where ios::end works fine. But in append mode you *cannot* move the put-pointer using seekp(). If you do want to use it kindly open the file explicitly in in|out mode.

Which part. I apologize I missed that you were using seekg() where ios::end works fine. But in append mode you *cannot* move the put-pointer using seekp(). If you do want to use it kindly open the file explicitly in in|out mode.

Thanks very much for the info. :)

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.