Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F78932547
ch-03-ex-02-solution.cpp
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
Fri, Aug 23, 20:21
Size
1 KB
Mime Type
text/x-c
Expires
Sun, Aug 25, 20:21 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
20124133
Attached To
R9360 PCSC_git
ch-03-ex-02-solution.cpp
View Options
/*
* chapter-03-exercise-02.cpp
*
* Count number of rows in a file
*
* Created on: Sep 2, 2012
* Author: Radu Popescu <radu.popescu@epfl.ch>
*/
#include <fstream>
#include <iostream>
#include <string>
int
main
(
int
argc
,
char
*
argv
[])
{
std
::
ifstream
read_file
(
"x_and_y.dat"
);
if
(
!
read_file
.
is_open
())
{
std
::
cout
<<
"Error opening file."
<<
std
::
endl
;
return
1
;
}
int
number_of_rows
=
0
;
// This loop will read an empty line between the final EOL and the EOF
// characters
while
(
!
read_file
.
eof
())
{
double
dummy1
,
dummy2
,
dummy3
,
dummy4
;
read_file
>>
dummy1
>>
dummy2
>>
dummy3
>>
dummy4
;
number_of_rows
++
;
}
std
::
cout
<<
"Number of rows = "
<<
number_of_rows
<<
" (false)"
<<
std
::
endl
;
// We try again
number_of_rows
=
0
;
read_file
.
clear
();
read_file
.
seekg
(
std
::
ios
::
beg
);
while
(
!
read_file
.
eof
())
{
std
::
string
line
;
getline
(
read_file
,
line
);
if
(
line
.
compare
(
""
)
!=
0
)
{
number_of_rows
++
;
}
}
std
::
cout
<<
"Number of rows = "
<<
number_of_rows
<<
" (true)"
<<
std
::
endl
;
read_file
.
close
();
return
0
;
}
Event Timeline
Log In to Comment