getline()

This is a discussion on getline() within the C++ Programming forums, part of the General Programming Boards category; I need to read in a line, but the problem is getline requires a size. So how could I get ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    94

    getline()

    I need to read in a line, but the problem is getline requires a size. So how could I get it to read a line when the situation is: char *txt and the size is not known.

    I probably need to overload the operator>> but how??? I dont know the size.


    Thanx
    Sophie
    simple is always an understatement.....

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    If you are using char* txt, you must allocate space for the string. You can do this either using new or on the stack (e.g. char txt[100];). Once you have allocated space for your string, then that is the number you pass to getline, because that is the maximum number of characters that your string can hold.

    By the way, the standard string class is in my opinion easier to use than char* style strings. With those, you don't have to allocate space or pay attention to maximum characters that will fit.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >char *txt and the size is not known.
    Change char * to std::string and use getline.
    Code:
    #include <string>
    ...
    std::string txt;
    ...
    getline ( in, txt );
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    borland 4.5 does not allow the use of non .h libraries so I am stuck...I have just recently installed .net at home but because I have never used it before, I dont know how to compile on it.
    simple is always an understatement.....

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >borland 4.5 does not allow the use of non .h libraries
    Borland offers their 5.5 version for free download. If this is your choice then upgrade. If it's not your choice then b1tchslap the person making the decisions and tell them to upgrade.

    >so I am stuck...
    No, you just have to do it the hard way. Use a fixed size buffer and read until it's full, copying into a dynamic array. Then resize the array and do the same thing. Repeat until you have the entire string. This method is awkward, bloated and unsafe, but it works if you have the time to debug it.

    >I dont know how to compile on it.
    My copy of VC++.NET came with a manual, how about yours?

    p.s. "I am silly"? That's about the dumbest censor replacement I've ever seen.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 10:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21