Author Topic: using the getline() function  (Read 4816 times)

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
using the getline() function
« 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++;
}
Analyze. Design. Develop. Debug. Deploy. Maintain.

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
using the getline() function
« Reply #1 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.
Gillius
Gillius's Programming http://www.gillius.org/

encargues_miguelerhlich

  • User
  • *
  • Posts: 6
    • http://bsit.bravehost.com
getline()
« Reply #2 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!!!
Miguel ErhlichD. Encargues

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
your own getline()
« Reply #3 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.
Analyze. Design. Develop. Debug. Deploy. Maintain.