#include<iostream>
#include<fstream>
using namespace std;

void main(){
ifstream inf("data.txt");
char name[30];

while(!inf.getline(name, 30, '|').eof())
{

char jersey_number[10];
char best_time[10];
char sport[40];
char high_school[40];
inf.getline(jersey_number, 10, '|');// #read thru pipe
inf.getline(best_time, 10); // #read thru newline
inf.getline(sport, 40, '|'); // #read thru pipe
inf.getline(high_school, 40); // #read thru newline

cout<<"jersey number: "<<jersey_number<<endl;
cout<<"best time: "<<best_time<<endl;
cout<<"sport: "<<sport<<endl;
cout<<"high school: "<<high_school<<endl;
}

Given data file: John|83|52.2
swimming|Jefferson
Jane|26|10.09
sprinting|San Marin

Anyone can explain how the above program work?
i get this from internet source
It will be a great help in doing my assignment.
The problems that avoid me from understanding the syntax
are:
how the getline function that take in 3 arguements
work?
What while(!inf.getline(name, 30, '|').eof()){} actually
do?
Urgent...please help...

Recommended Answers

All 3 Replies

istream::getline (both the two arg and the three arg versions) fill a c-style array with characters read from an input stream. characters are extracted upto a maximum of n-1 (where n is the second arg) or the delimiter (defaults to a newline in the two arg version, the third arg in the three arg version) is encountered. if the delimiter is found, it is read and discarded (ie. the next char read would be the one after the delimiter). an ending null character that signals the end of a c-string is automatically appended.

if the stream encounters an error during input (typically eof), the state of the stream is set to some not good state. getline returns a reference to the stream; so
while(!inf.getline(name, 30, '|').eof())
means 'while the state of the stream is not eof'.
while( inf.getline(name, 30, '|') ) would have been more accurate; this would mean
'while the state of the stream is good'

Member Avatar for iamthwee

I think the short answer is why you would want to understand a piece of code that sucks so much.

commented: Agreed. --joeprogrammer +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.