Thread: can't read whole line from file

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    20

    can't read whole line from file

    I'm using Bloodshed's Dev-C++ 4.9.8.0


    Code:
    from custom.h header:
    
    #include <windows.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    struct PLAYER
    {
        string Name;
        int Gold;
        int Hp;
        XP Xp;
        WEAPON Weapon;
        ARMOR Armor;
    };
    
    from main.cpp:
    1   #include "custom.h"
    2
    3   int main()
    4   {
    5       PLAYER Player;
    ...       
    23      ifstream loadpname;
    24      loadpname.open("playername.txt");
    25      loadpname.getline(Player.Name, 50, '\n');
    26      loadpname.close();
    27
    28      return 0;
    29  }
    Why wont the above read the first line in playername.txt?
    The compiler says this:

    25 C:\programs\Spil\main.cpp
    no matching function for call to `std::basic_ifstream<char,

    664 C:\Dev-Cpp\include\c++\bits\istream.tcc
    candidates are:

    176 C:\Dev-Cpp\include\c++\istream
    std::basic_istream<_CharT,

    doesn't getline(blah) take strings?

    if it doesn't then how do I determine how long the line is? So I
    can use an array. I can't seem to find how to do it anywhere...
    Last edited by Xarr; 06-12-2004 at 01:18 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    ifstream.getline() takes char* not string.
    The non-member function getline() takes an input stream and a string.
    Code:
    getline(loadpname, Player.Name);
    gg

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    20
    Thx that helped

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OPen a file and read it from the last line
    By c_geek in forum C Programming
    Replies: 14
    Last Post: 01-26-2008, 06:20 AM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. getline function to read 1 line from a text file
    By kes103 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 06:21 PM
  4. how can i read i line from a file
    By condorx in forum C Programming
    Replies: 2
    Last Post: 05-07-2003, 02:47 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM