You are here:

C++/file handling in C++

Advertisement


Question
I have made a generic template class for  1D array.This array can be any type of inbuilt data type..I have to save and load the object of class in a file.Please tell  me how to make these functions. Also answer me this probliem in context of a class only having integer type 1 D array

Answer
Hello shruti, thank you for the question.

If you want to save an array to a file, here is some pseudocode.

// Open the file for writing

// loop over the array and write each value
// (array[i]) to the file with a space in between

// Close the file

Here is pseudocode for reading in from a file

// Open the file for reading

// Loop until you get the eof()
// ( while(!ifstreamObject.eof()) )

// Count the number of values in the file
// to get the array size

// Dynamically allocate your array

// Read in the data values

// Close the file

This should be enough to help get you started. Please feel free to ask another question if you have problems.

I hope this information is helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.