Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File I/O Problem

Posted on 2000-03-01
5
Medium Priority
?
206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 360 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

705 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