Solved

File I/O Problem

Posted on 2000-03-01
5
192 Views
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.
0
Comment
Question by:benny_sisko
  • 3
5 Comments
 
LVL 1

Expert Comment

by:ntdragon
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
0
 
LVL 22

Accepted Solution

by:
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.

continues.
0
 
LVL 22

Expert Comment

by:nietod
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.

continues.
0
 
LVL 22

Expert Comment

by:nietod
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;
OutFile.write(&x,sizeof(int));
InFile.read(&x,sizeof(int));

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.
0
 

Author Comment

by:benny_sisko
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.

Thanks.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

713 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