CcodeTemplates
//========reading a complete binary
file========
#include
<iostream>
#include
<fstream>
using
namespace std;
ifstream::pos_type size;
char
*
memblock;
int
main ()
{
int
i ,j , h, l , z , m, s;
ifstream file
("/Users/donsauer/Desktop/cmdtests/3N45E007.hgt",
ios::in|ios::binary|ios::ate);
ofstream myfile
("/Users/donsauer/Desktop/1.txt");
if
(file.is_open())
{ size
=
file.tellg();
cout
<< " file size is " << size ;
memblock
= new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
cout
<< " the complete file content is in memory \n" ;
s=
3;
m
=
1201*s ; // 7200 points per row file 25934402
is 3602X7200
m
=
3601;
for
(j=0; j< 180*2; j=j+4)
{
for
(i=2250; i< 2460; i=i+2)
{ h
=
(int) memblock[ 2*i +m*j];
if
( h < 0 ) h=h+256;
l
=
(int) memblock[2*i+1 +m*j];
if
( l < 0 ) l=l+256;
z
=
(256*h +l)-2000;
myfile
<< z << " " ;
}
myfile
<< "\n" ;
}
myfile.close()
delete[] memblock;
} else
cout << "Unable
to open file";
return
0;
}
//========reading a complete text
file========
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile
("/Users/donsauer/Desktop/zzzz/cmdtests/example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to
open file";
return 0;
}
//========write a complete text
file========
#include
<iostream>
#include
<fstream>
using
namespace std;
int
main (int argc, char * const argv[])
{ ofstream myfile
("/Users/donsauer/Desktop/cmdtests/example.txt");
if
(myfile.is_open())
{ myfile
<< "This is a line.\n";
myfile
<< "This is another line.\n";
myfile.close();
} else cout << "Unable
to open file";
return 0;
std::cout <<
"Hello, World!\n"; // insert code here...
return 0;
}
//========obtain file size========
// obtaining file size
#include
<iostream>
#include
<fstream>
using
namespace std;
int
main ()
{
long
begin,end;
ifstream
myfile ("/Users/donsauer/Desktop/cmdtests/example.txt");
begin
=
myfile.tellg();
myfile.seekg (0, ios::end);
end
=
myfile.tellg();
myfile.close();
cout
<< "size is: " << (end-begin) << " bytes.\n";
return
0;
}