file io operation help

hello,

i would like to know how to read characters from a file which have more than one line for example this file "one.txt"
"one.txt"
abcdefg
123456789
qwerty
asdfgh
;lkjhj


the books i have seen have only given me the method to read the first line using
1
2
3
4
string input;
ifstream fin;
fin.open("one.txt");
getline(fin,input);

now if i want to read the second or the third line what should i do.
closed account (zb0S216C)
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( );
Last edited on
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
closed account (zb0S216C)
Then you need a loop external to the method, not within the method. Do you mean something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string Get_line( void )
{
    string input;
    ifstream fin;
    fin.open( "one.txt" );
    if( fin.is_open( ) == false ) 
    {
        cout << "Failed to open the file" << endl;
        return "NULL";
    }

    getline( fin, input );
    fin.close( );
    return input;
}

for( short Line( 0 ); Line < 5; Line++ )
{
    Get_line( );
}

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.
Last edited on
do you mean this is better....
but fin is defined in main can i acces it in get_line()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
string Get_line( void )
{
    
    if( fin.is_open( ) == false ) 
    {
        cout << "Failed to open the file" << endl;
        return "NULL";
    }

    getline( fin, input );
    return input;
}

int main()
{
    string input;
    ifstream fin;
    fin.open( "one.txt" );

for( short Line( 0 ); Line < 5; Line++ )
{
    Get_line( );
}

}
closed account (zb0S216C)
but fin is defined in main can i acces it in get_line()

You need to pass your working instance of ifstream as a parameter then. Simply replace void with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string Get_line( ifstream &Instance )
{
    // Method body code...
}

int main( )
{
    ifstream fin;

    // ...

   for( short Line( 0 ); Line < 5; Line++ )
   {
        Get_line( fin );
   }
}
Last edited on
thank you
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....
closed account (zb0S216C)
Does each line start with a specified character or number? Here's an example file to show you what I mean:
LINE 8 ...
LINE 2 ...
LINE 3 ...

If not, can you post a sample line?
Last edited on
what i was trying to say is there should not be any restriction to the file being read any file even the source file or any html or arbitrary file....


maybe say this file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
========================================================================
    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.

///////////////////////////////////////////////////////////////////////////// 
in this code here
1
2
3
4
5
6
7
8
9
10
11
int main()
{
..
     string filename,datain;
     cout << "enter filename:";
     getline( cin , filename )
     ifstream fin;
     fin.open( filename.c_str() );
     getline(fin , datain);
...
}

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?
Last edited on
Topic archived. No new replies allowed.