A simple loop would do the job. Here's a quick example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
string input[ 5 ]; // Making 'input' an array is optional. However, every extracted line will overwrite the last line.
ifstream fin;
fin.open( )
if( fin.is_open( ) == false )
{
cout << "Failed to open the file" << endl;
return 0;
}
for( short Line( 0 ); Line < 5; Line++ )
{
getline( fin, input[ Line ] );
}
//...
fin.close( );
but the problem here is i am using fin.open( ) and fin.close() inside a function. and i want that function to return the next line every time i call it
Note: Constantly opening and closing a file is slow. Creating the input buffer with each call maybe considered slow. It's best to open the file externally( outside the method body ) and create the input buffer externally as well. That way, with each call of Get_line( ), the input buffer is pre-made and the file is already open.
one last thing,
the sizes of files may vary, like one file may contain just 2 lines and another may contain 10 lines how to make the above program such that it automatically detects the end....
========================================================================
CONSOLE APPLICATION : calculator Project Overview
========================================================================
AppWizard has created this calculator application for you.
This file contains a summary of what you will find in each of the files that
make up your calculator application.
calculator.vcproj
This is the main project file for VC++ projects generated using an Application Wizard.
It contains information about the version of Visual C++ that generated the file, and
information about the platforms, configurations, and project features selected with the
Application Wizard.
calculator.cpp
This is the main application source file.
/////////////////////////////////////////////////////////////////////////////
Other standard files:
StdAfx.h, StdAfx.cpp
These files are used to build a precompiled header (PCH) file
named calculator.pch and a precompiled types file named StdAfx.obj.
/////////////////////////////////////////////////////////////////////////////
Other notes:
AppWizard uses "TODO:" comments to indicate parts of the source code you
should add to or customize.
/////////////////////////////////////////////////////////////////////////////
the getline(fin,datain) from file will read one line without the newline '\n' character.
how do i make it such that it'll also read the '\n' character..?
is there any modifier?