Reading selectively from files. The function .getline(
) :
In many instances of reading data from a file we must skip several lines
of text and zoom into the lines which contain the numerical data we need.
An easy device for achieving this is the I/O stream member function
.getline which is used in conjunction with a file stream. The arguments
of the function .getline are a) a character array and b) the size of the
array.
The following example illustrates the use of this function.
Place
your mouse in the areas below to see comments about the corresponding sections
of code
Code
/* An example on the use of .getline() : file: 6ex6.cpp FALL 1998 ___________________________________ Jacob Y. Kazakia jyk0 October 21, 1998 Recitation Instructor: J.Y.Kazakia Recitation Section 01 ___________________________________
Purpose: This program reads from a file named 6ex6data.txt.
It uses the function .getline to skip the first
few lines of the file and then reads the second
line of the numerical data.
cout << " \n speed in meters per second " << speedMPS ; cout << " \n speed in miles per hour " << speedMPH ; cout << " \n drag force in Newtons " << dragNEWTONS ; cout << " \n drag force in pounds " << dragPOUNDS ;
cout<<" \n\n enter e (exit) to terminate the program...."; char hold; cin>>hold; }