Solved

File I/O Problem

Posted on 2000-03-01
5
181 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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

10 Experts available now in Live!

Get 1:1 Help Now