Lab11 - if(i 1)==0 file2.tellg file.close/closes the file cout<<endl c/Question 2 float ptr2 float y[100 for(i = 0 i<100 i/initializes the

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include<iostream> #include<fstream> # using namespace std; char *memblock; fstream::pos_type size; fstream::pos_type size2; f void main() { //Question 1 int* ptr1; int x[100]; for(int i=0; i<100; i++) //initializes the values of each element in the array { x[i]=i; if((i+1)%10==0) cout<<x[i]<<endl; //prints every 10th element } ptr1=&x[0]; //memory address of the start of the x array p fstream file("integer.dat",ios::out|ios::binary|ios::ate); fstream file2("integer.dat",ios::in|ios::binary|ios::ate); f if(file.is_open()) //tests whether the file was successfully opened { size=file.tellp(); //returns the current position of the put pointer size2=file2.tellg(); //returns the current position of the get pointer memblock=new char[size]; file.seekp(0,ios::beg); //moves the position pointer to the beginning file.write(memblock,size); //writes the values into "integer.dat" f for(i =0; i<100; i++) { file2.read(memblock,size); //reads every 10th element of the array because of the loop
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if((i+1)%10==0) file2.tellg(); } file.close(); //closes the file } cout<<endl; c //Question 2 / float* ptr2; float y[100]; for(i = 0; i<100; i++) //initializes the values of each element in the array { y[i]=float(i); if((i+1)%10==0) cout<<y[i]<<endl; //prints every 10th element } ptr2=&y[0]; //memory address of the start of the y array p fstream file3("float.dat",ios::in|ios::binary|ios::ate); if(file3.is_open()) //tests whether the file was successfully opened { size=file3.tellp(); //returns the current position pointer of the put pointer memblock=new char[size]; file3.seekp(0,ios::beg); //moves the position pointer to the beginning file3.write(memblock,size); //writes the values into "float.dat" f for(i =0; i<100; i++) { file3.read(memblock,size); //reads every 10th element of the array because of the loop if((i+1)%10==0) file3.tellp(); } file.close(); //closes the file } system("pause"); }...
View Full Document

This note was uploaded on 04/25/2010 for the course CECS CIS 200 taught by Professor Jieshen during the Winter '08 term at University of Michigan-Dearborn.

Page1 / 2

Lab11 - if(i 1)==0 file2.tellg file.close/closes the file cout<<endl c/Question 2 float ptr2 float y[100 for(i = 0 i<100 i/initializes the

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online