how to read data in text file?

Hi there, I need help in reading a list of data in a text file. What function do we use in order to read the list of data?
Depends on what you are reading. operator>> or getline can be used for most things
http://www.cplusplus.com/doc/tutorial/files/
I am sorry. I am new in C++. I only do C last year. But may I ask what does getline do? Does it read one row at a time? or does it read the whole data in the text file? I am quite confuse about this.

Thanks.
The name getline suggests that it reads line by line from the file. The question is how do you identify the end of a line? Well it is identified by the newline character (ascii 10) which should be present at the end of each row. You can also use fgets. The best way of getting your answer is to write a small test program.
getline reads every character before a delimiter ( which is by default the newline character )
eg:
1
2
3
4
// file is a ifstream, s a string
getline ( file, s ); // reads a line from the file ( up to \n )
getline ( file, s, '\n' ); // same as above
getline ( file, s, ',' ); // reads until it finds a comma 
If for example, below is the text file:

i No
1 123456789
2 123456788
3 123456777

The question here is that, how do I read the last line on the text file? Is it the same as
getline( file, s) ? or do I have to use a pointer too?





If you have numbers, use operator>>
if you need store the all lines below is the code also:
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
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
std::string getLast(const std::vector<std::string> &);
int main(void)
{
        std::ifstream readline("text.in", std::ifstream::in);

        std::vector<std::string> lines;
        if (readline) {
                std::string s;
                while (getline(readline, s))
                        lines.push_back(s);
        }
        std::cout << getLast(lines) << std::endl;
        readline.close();
        return 0;
}
std::string getLast(const std::vector<std::string> &lines)
{
        std::vector<std::string>::const_iterator iter = lines.end() - 1;

        return *iter;
}

I will try that code sometimes later once I solve my problem :) Anyway thanks for the help.
Topic archived. No new replies allowed.