0

i want to read a file and put each line in a string (each line contains a single word) i've used getline but it doesn't work neither does the >> command. here's my code: (i'm using visual studio)

string device_kind;
ifstream bank_info;
bank_info.open ("acquirer.info");

bank_info >> device_kind; //fails to compile
getline (bank_info, device_kind); //also fails

bank_info.close();
user3033190
  • 11
  • 1
  • 1
  • 1
    "fails to compile" isn't a very useful problem description. Your compiler provides more details than that. – molbdnilo Feb 06 '14 at 08:20
  • `bank_info >> device_kind` shouldn't fail to compile. Or do you mean it does not work like you wanted it? Make sure you have included ``! – mb84 Feb 06 '14 at 10:02

2 Answers2

0

use bank_info.geline(device_kind,size) getline is member function of ifstream so use it with . operator.

Amit Chauhan
  • 6,151
  • 2
  • 24
  • 37
  • There's also a global C++ function `std::getline()`, the one the OP is using. The member function version you're using is for C-style arrays, that line of code will not compile. – David G Feb 06 '14 at 13:09
0

Look at my code on IDEONE

string device_kind;
ifstream bank_info("acquirer.info");

if(!bank_info.bad())
{
   getline(bank_info, device_kind);
   cout << device_kind;
}

bank_info.close();

It outputs the first line of the file for me, so it should also work for you!

If you want to read your file in a vector<string> of lines, you can do it, like I do here: http://ideone.com/qNW5N9

Victor
  • 13,914
  • 19
  • 78
  • 147