Thread: Quick Question about getline

  1. #1
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96

    Quick Question about getline

    I some code i was trying to debug i had the lines:
    Code:
    objfile.getline(str,10);
    cout << str <<endl;
    
    objfile.getline(str,10);
    cout << str <<endl;
    Which does not over the entire lenth of the string (char str[50]; )
    I was testing to see if getline would either put out the same string twice or would automaticly carry down weather or not it reached the and of the string or not, however...

    The output was neither, insted it only puts out the 1st 10 chars and then a carrage return
    then is blank and carrage returns again.

    Why does it clear str insted of filling it with text the 2nd time if the end of string wasn't reached in the 1st?
    Last edited by Matty_Alan; 04-23-2010 at 02:04 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you should try:
    Code:
    objfile.getline(str,10);
    cout << '[' << str << ']' << endl;
    
    objfile.getline(str,10);
    cout << '[' << str << ']' << endl;
    What is the input?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    it's just reading an .obj file, im attamping to write a loader for a bit of experiance

    the prticular line im reading is
    "# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware"
    Last edited by Matty_Alan; 04-23-2010 at 02:44 AM.

  4. #4
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    Quote Originally Posted by laserlight View Post
    Perhaps you should try:
    Code:
    objfile.getline(str,10);
    cout << '[' << str << ']' << endl;
    
    objfile.getline(str,10);
    cout << '[' << str << ']' << endl;
    What is the input?
    thats just giving me:
    Code:
    [# 3ds Max]
    []

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah. The problem is that when getline reads 9 characters without reaching the delimiter, i.e., the newline character, it causes the stream to enter a fail state.

    I'd say that a simple solution is to use a std::string instead of an array of 50 char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick question (adding characters)
    By Cactus in forum C Programming
    Replies: 2
    Last Post: 09-24-2005, 03:54 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM