File I/O Problem

Posted on 2000-03-01
Last Modified: 2010-04-02
I'm working on a programming assignment on OpenGL using C++.  The assignment involves drawing a 3d image and texture mapping it using vertex and polygon data from a file.

The problem is that my solution involves saving the position of the pointer in the file performing other operations later on in the file and then returning back to the saved position. I am using something like below:

ifstream InFile("filename");
streampos pos;

//I input the data as following
float f1;
InFile >> fl;

//at the point I want to save the file pointer

pos = Infile.tellg();

For some reason this does not work properly. A small section showing the layout of the data file is shown below:

0 -0.2048 -0.090616 0 -0.204 -0.090824 0 -0.2032 -0.09104
0 -0.2024 -0.091256 0 -0.2016 -0.091464 0 -0.2008 -0.09168

The file size is about 2MB.
I'm using Visual Studio in Win 98

Any help appreciated.
Question by:benny_sisko
  • 3

Expert Comment

ID: 2576146
first of all i don't remember what is streampos and as i remember tellg() returns int

second it's not good to save the coordiante of your object 'cause it demage your object if you want to save then save the matrix not the coordiantes of the vertixs and that you can do without files using glPopMatrix and glPushMatrix

and is that all you code or there is more if there is show as more of your code
LVL 22

Accepted Solution

nietod earned 120 total points
ID: 2576180
benny, the most likely problem is that you later "back up" to this saved position and write data to it, but the data you are writing iver goign to over write what was there before and either A) you didn't realize it was overwritting infomrmation or b) you realized it was overwritting but didn't take steps to insure that the overwritten information was of the exact same length as the invofmration you are writing.

LVL 22

Expert Comment

ID: 2576192
Consider, the following file of 3 integers

1 234 5

If I "back up" to the 2nd integer and try to change it to a '8' what to I get as a result?

1 834 5

That isn't right.

So when "backing up" and writting information you must take steps to insure that the new information is the exact same length as the old information it replaces.  This can be done in ASCII mode, but it is hard to do.  Like above you could take steps to insure that each number id exactly 6 characters long (like by using setw()) so when you replace a number you just need to make sure the replacement is also 6 characters long.  But this can be messy.

A better solution is that if you well be performing random access (moving back and forth in a file) you should store the information in a binary format.

LVL 22

Expert Comment

ID: 2576200
For this you should open the file in binary mode using the ios_base:bin attribute like

ifstream InFile("filename",ios_base::bin);

Then you write integers and floats using the write() member function and read them with the read() member function, like

int x = 1243;

The advantage to this approach is that in a binary format, the numbers will always be the same length, regardless of the magnitude of the value they store.    This means that the position of information in the file doesn't change as the numbers stored in the file change.

Let me know if you have any questions.

Author Comment

ID: 2578474
Thanks for the answer. As it happens I have solved the problem without using random access. However, I will be implementing your method later on when I make my program more general. If I have any questions i'll contact you.


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now