Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F102842002
ex2.cc
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Feb 24, 18:07
Size
1 KB
Mime Type
text/x-c
Expires
Wed, Feb 26, 18:07 (1 d, 20 h)
Engine
blob
Format
Raw Data
Handle
24439798
Attached To
R7571 SP4E-TB-TL-FR
ex2.cc
View Options
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <iostream>
/* -------------------------------------------------------------------------- */
void
write
(
const
char
*
filename
,
double
x
,
double
y
,
double
z
)
{
// write 3D point to file
std
::
ofstream
fout
(
filename
);
std
::
cout
<<
"Writing coordinates ("
<<
x
<<
", "
<<
y
<<
", "
<<
z
<<
") into "
<<
filename
<<
std
::
endl
;
fout
<<
x
<<
" "
<<
y
<<
" "
<<
z
;
fout
.
close
();
}
void
read
(
const
char
*
filename
)
{
// read 3D point from file
std
::
ifstream
fin
(
filename
);
std
::
stringstream
ss
;
ss
<<
fin
.
rdbuf
();
double
x
,
y
,
z
;
ss
>>
x
>>
y
>>
z
;
std
::
cout
<<
"Reading coordinates from "
<<
filename
<<
": ("
<<
x
<<
", "
<<
y
<<
", "
<<
z
<<
")"
<<
std
::
endl
;
}
int
main
(
int
argc
,
char
**
argv
){
const
char
*
filename
=
"toto.txt"
;
double
x
,
y
,
z
;
if
(
argc
==
4
)
{
// x, y, z provided
x
=
atof
(
argv
[
1
]);
y
=
atof
(
argv
[
2
]);
z
=
atof
(
argv
[
3
]);
}
else
if
(
argc
==
1
)
{
// x, y, z not provided
x
=
1.7
;
y
=
1.0
;
z
=
2.0
;
}
else
{
std
::
cout
<<
"
\033
[31m"
<<
"Invalid number of arguments: "
<<
argc
<<
"
\033
[0m"
<<
std
::
endl
;
return
0
;
}
write
(
filename
,
x
,
y
,
z
);
read
(
filename
);
return
EXIT_SUCCESS
;
}
Event Timeline
Log In to Comment