# is put and get file pointer of class fstream are equal

This topic is 4756 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

const int ENORMOUS=512;
{
for(unsigned int cntr2=0; cntr2<ENORMOUS; ++cntr2)
{
if(*(MainBuffer+cntr2)=='{')
{
// InputOutput nesnesinin put dosya iþaretçisini bir sonraki konuma ayarla..
FileInputOutputObj.seekp( (cntr1*ENORMOUS)+cntr2+1,ios::beg);
cout<<"\nDosya Put isaretcisi:"<<FileInputOutputObj.tellp();

cout<<"\nDosya Get isaretcisi:"<<FileInputOutputObj.tellg();


i have an object call FileInputOutputObj (an instance of fstream) ok now my problem is i read ENORMOUS byte into MainBuffer in every cycle of first for loop.Bu unfortinatly The get pointer of FileInputOutputObj is getting equal i used cout<<tellp() and cout<<tellg() for that to see the result.. is get & put pointer of fstream must be equal when opening & writing into the same file...?i mean i want to read the start of file and write to the end of it i learned that they are not equal..but it seems that they are because my get pointer isnot increasing after the read function and tellp() and tellg() are returning the same value...

##### Share on other sites
Well can you please explain what you want to accompish? You are mixing a lot of function that will not behave nicely with each other in the format you have. If you are reading from a file and writing to it at the same time, then you must keep track of both positions. Not only that, using the read function in the conditional statement of the for loop is bound to have some side effects. What are your goals of reading and writing at the same time?

##### Share on other sites
well i have .sub file (which is actuall a .txt file for .divx format) but matter is have to -10000 the starting frame of every second .(so i didn't want to do that manually) i decided to write a program a .sub file is just something like that:

{10234} {10403} it is good to see you here blablabla....
{10543} {10749} well may be not....

where {10234} is beginning frame and {10403} is the ending fram of sentence

i decide to write a program that takes the value int { } and modify it then rewrite it to the file.. unfortanately there are some mistakes
here is the code if you care
#include <iostream>#include <fstream>#include <conio.h>using namespace std;const int MAX=80;const int ENORMOUS=512;char MainBuffer[ENORMOUS]; //for reading filechar EditBuffer[ENORMOUS]; //for editing and modifiyng the content of filevoid InitializeBuffer(char* BufferName,int BufferSize ,char FillChar=NULL){	for(register int cntr1=0;cntr1<BufferSize;++cntr1)		*(BufferName+cntr1)=FillChar;}int main(int argc,char* argv[]){/*	if(argc<2)	{		cout<<"Press AnyKey to exit.Because program needs a sss.txt file to modify";		getch();		return 0;	}*/	fstream FileInputOutputObj;	FileInputOutputObj.open(/*argv[1]*/"sss.txt",ios::in|ios::binary|ios::out|ios::app);	if(!FileInputOutputObj)	{		cout<<"\nAn Error Occured Press Anykey to exit";		getch();		return 0;	}	InitializeBuffer(MainBuffer,ENORMOUS);	InitializeBuffer(EditBuffer,ENORMOUS);	FileInputOutputObj.seekg(0,ios::beg);	FileInputOutputObj.seekp(0,ios::beg);	for(unsigned int cntr1=0;FileInputOutputObj.read(MainBuffer,ENORMOUS);++cntr1)	{		for(unsigned int cntr2=0; cntr2<ENORMOUS; ++cntr2)		{			if(*(MainBuffer+cntr2)=='{')			{				// InputOutput nesnesinin put dosya iþaretçisini bir sonraki konuma ayarla..				FileInputOutputObj.seekp( (cntr1*ENORMOUS)+cntr2+1,ios::beg);				cout<<"\nDosya Put isaretcisi:"<<FileInputOutputObj.tellp();                cout<<"\nDosya Get isaretcisi:"<<FileInputOutputObj.tellg();				//getche();				for(unsigned int cntr3=0; true;++cntr3)				{					if(*(MainBuffer+cntr2+cntr3+1)=='}')					{						*(EditBuffer+cntr3)=NULL; //from now on we get the number as a cstring						break;					}					*(EditBuffer+cntr3)=*(MainBuffer+cntr2+cntr3+1); //copy the content;				}				//cout<<EditBuffer<<endl;				//getch();				//we must make the cstring a number and modify it..				unsigned int Tempint=atoi(EditBuffer); //we made it number;				Tempint-=10000; //WE MADE OUR MODIFICATION				cout<<"bu kelime: "<<itoa(Tempint,EditBuffer,10)<<endl;				//cout<<"\nSayi :"<<Tempint<<endl;				//now because of we adjusted the put file pointer of InputOutputObj we can write..				if(!(FileInputOutputObj.write(EditBuffer,strlen(EditBuffer))))				{					cout<<"\nCouldn't write to file";				}			}			//FileInputOutputObj.seekg((cntr1*ENORMOUS)+ENORMOUS,ios::beg);			//cout<<"\nBu yeni:"<<FileInputOutputObj.tellg()<<endl;		}		InitializeBuffer(MainBuffer,ENORMOUS);		InitializeBuffer(EditBuffer,ENORMOUS);	}	//getch();	return 0;}

##### Share on other sites
Ok I am not familiar with those files, but here's an idea of what you should try. I assume those sub files are not large at all. So how about this:
1. Open the file using ifstream2. Read in the contents and store them in a string3. Close the file4. Make the updates you will want5. Open a new file using ofstream6. Write out the new sub file

Now you will be able to work with the entire file itself in a string format rather than having to try and mess with it with fstream. All of the work you will be doing is at step 4. You will process the { # } and do what ever you want with it. Then after you are done, you just open up a new file and write it out.

I think I just realized what you are trying to do - correct me if I'm wrong, you want to literally minus out '10000 from each number. Right? I did not really see what you mean by that at first until now.

If you can post a sample of that sub file and how it looks I think I can give you a few more tips of what you can try, but I'd say definitly take the approach of loading into a string first. It will make your life easier.

- Drew

##### Share on other sites
Drew thanks so much.. i will send you the file..but i am also in aware of the
ifstream/ofstream method .The matter is it takes some Kb's and i just wanted to make the program in best way(it is not important to -10000 all literals in a esasier way i can do that with manually also) matter is to have a program that can manipulate the frame rates for all .sub files (and manipulates them in least effort)
So my aim was to accomplish best price/performace algorithm.And i want to use fstream for every file that iwill read & write at the same time.That is a choice ...doing it in most appropriate form..

and there is only one problem with that code InputOutputObj.read() is not increasing the Get pointer of file ? i just don't know why?
i wish someone tell me why is that happening? i couldn^t realize where the matter is..

do you still want to have .sub file

##### Share on other sites
Ok if you still want synchronized I/O for the files to save space and efficiency, you will still need to use 2 files. Basically you can read in your block, modify it, the send it out to the other file. Working on the same file with I/O simutaeously is not something I think is very easy or efficient. This will keep to your goal of the best price/performace algorithm because as you read in 512, you will write out that 512 to a new file in order. I just think taht would be the best way to do this, but if you still wanted to do the one file that is still fine. [smile]

However there is one big problem I can see. You are working with a binary file, as you opened with "ios::binary". I do not know if you can directly work with the file that way in binary format. That could be why there is a problem. Looking at your OP, I would suggest a little change.
const int ENORMOUS=512;unsigned int cntr1 = 0;while( FileInputOutputObj.read(MainBuffer,ENORMOUS) ){   for(unsigned int cntr2=0; cntr2<ENORMOUS; ++cntr2)   {      if(*(MainBuffer+cntr2)=='{')      {            cout<<"\nDosya Put isaretcisi:"<<FileInputOutputObj.tellp();            cout<<"\nDosya Get isaretcisi:"<<FileInputOutputObj.tellg();            FileInputOutputObj.seekp( (cntr1*ENORMOUS)+cntr2+1,ios::beg);      }   }   cntr1++;}

I do not know if that will fix your problems but to answer your questions why they are the same, after you read the file, the FP is set to some position X. However, you were calling "seekp" before you outputted. That's why it was the same. Instead, I moved the seekp after the outputting, now it should report the right thing. You can try and make that change in your code and see of it outputs the right thing now.

As for the .sub file, is it a text file or binary file? If it is a text file, you do not need to open it in binary format. If not then it is fine as is. I'd be intrested in seeing that .sub file anyways. You can look at my profile for my email address.

- Drew

• 10
• 11
• 22
• 11
• 15