Gillius's Programming Forums

Hosted Forum: West Visayas State University - Philippines => Problem Solving and Programming Class Forum => Topic started by: louiecerv on February 23, 2006, 04:00:00 PM

Title: using the getline() function
Post by: louiecerv on February 23, 2006, 04:00:00 PM
When you need to read data from an input file one line at a time, you can use the getline() function.

Here is a sample code that reads data from a file onto an array of characters:

Code: [Select]

char horoscope[12][256];
int line = 0;
char next;
while ( !fin.eof() && line < 12 )
{
          fin.getline(horoscope[line], 256, '\n');
line++;
}
Title: using the getline() function
Post by: Gillius on February 23, 2006, 07:19:08 PM
If the line is over 256 characters long, the parsing will not go as expected.

A better solution would be to use std::string and use the std::getline function that loads the line into the string, to prevent any possible memory problems.
Title: getline()
Post by: encargues_miguelerhlich on February 27, 2006, 12:56:56 AM
Good day!!

What if I will not use the getline() function is there any alternative codes for running a program??

Thanks!!!
Title: your own getline()
Post by: louiecerv on March 01, 2006, 06:58:27 AM
you can define your own getline function using a loop and the get() function.  There is a sample in our textbook showing how this can be done.