1

I have read that you can read a line of text from a text file using getline() when using fstream for file processing. How do you do that?

I have a file named accounts.txt. And in that I have-> name, age, balance I need to read the line balance?

Thank you.

NOT every line, The line which has the value of balance

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
PewK
  • 397
  • 3
  • 8
  • 19

1 Answers1

0

using getline with parameter as stream and string where to store the line.

getline(stream,line)

#include<iostream>
using namespace std;
#include<fstream>
int main()
{
    fstream fin("filename");
    string line;
    while(getline(fin,line))
    cout<<line<<endl;
}
Dineshkumar
  • 4,165
  • 5
  • 29
  • 43
  • You need `std::` for `fstream` and `string`. – jogojapan May 25 '13 at 05:45
  • 1
    I guess yall got the wrong idea. I need to read a specific line i.e: Username from the file – PewK May 25 '13 at 06:03
  • give sample input of file. this is just an idea for getline to be used. we need to parse the line. – Dineshkumar May 25 '13 at 06:07
  • 1) User enters username 2) Program reads the 4th line of the text file (i.e: username) 3) Checks if both usernames are the same 4) Success message – PewK May 25 '13 at 06:11
  • check in accounts that the username matches the input. – Dineshkumar May 25 '13 at 06:12
  • It is not possible since there are many accounts and each account username is stored in the text file, the program should check the file for the matching user input (username) and authenticate. suggestions? – PewK May 25 '13 at 06:15