C++ Object Oriented Programming (Binary Files)

Image result for binary files


Binary Files


Binary files contain unformatted and non-humanreadable data.

Specified by using a binary flag on open:
inFile.open("nums.dat", ios::in|ios::binary);



seekg(input) used to go to a specific location in a file open for input file.

seekp(output) used with files open for output

tellp  member function: return current byte position in output file

tellg member function: return current byte position in input file


Copy a file
Writing and reading an object into/from a file


Writing an array of objects into a file

Writing an array of objects into a file (2)

Reading an array of objects from a file

Once a time

Three at a time

Past Year Question
1718


a)What is the purpose of the program?

to access Binary Files which contain unformatted and non human readable data.

b)Describe the role of following line in program in term of their context meaning

20-21 - to declare a binary file,use binary flag an open function

23-24 -set read position 0 bytes,from end position
            return current byte position in input file.
26-Set read position 0 bytes from beginning position

29-30 - read fin file with address of 2000 and the size of n
            write fin file with address of 2000 and the size of n

p/s to read fin file the first argument is address which is the starting address of the section of memory that is to be written to the file,second is the size is number of bytes of memory to write

c)The program in facts faces a problem due to lines 27 to 29.Describe what problem is

to read and write non-character data,must use a typecast operator to treat the address of the data as a character address.

d)Rewrite the code for lines 27 to 29 to resolve problem
char data[20000]//array of object
fin.read(char*)data,sizeof(n));//kena ada sizeof,sebb nk kena tau size berapa
                                                //treat the adress of num as a char
                                                //tak perlu ampersend sbb dia array
fout.write(data,n); 

Post a Comment

0 Comments