Thread: Strange behavior in getline based writing to file.

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    1

    Strange behavior in getline based writing to file.

    In this simple little practice project I am doing to teach myself a little C++, my program skipping the first word to be output to the file, and then writing the next four characters, followed by nothing. I'm not sure what I'm doing wrong here. Anybody have any clue what's going on here?

    Code:
    /* Practice project in C++, a command line HTML editor. Project
    started on Jan. 27, 2013 at 12:18 AM. Yeah, I'm an owl. */
    
    
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    
    using namespace std;
    
    int main()
    
    {
        int count=0;
        int sheaderleng;
        string sheader;
    
        /*Here, the program asks the user to enter what the header will be
        for the title header that is to be used.*/
        cout<<"Enter header text.\n";
        cin>>sheader;
        
        //convert the string into a character array so that getline can accept it.
        
        sheaderleng=sheader.length();
        char header[sheaderleng];
            do
                {
                    header[count]=sheader[count];
                    count++;
                }while(count<=sheaderleng);
        
    //Write data to file.
        
        cin.getline (header,sheaderleng);
        ofstream outt("File1.html");
        outt<<"<html>"<<"\n"<<"<h1>"<<header<<"<h1/>"<<"\n"<<"<html/>";
        outt.close();
    
    //Finish program.
    
        return 0;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    >> //convert the string into a character array so that getline can accept it.

    Completely unnecessary. getline works with the std::string type. So you might as well just use it and forget the nonsense about copying it to a character array.

    As of now, your code has a warning because you tried to convert a C++ string to a C string:
    Code:
    mingw32-g++.exe -Wall  -g  -pedantic -std=c++98 -Wextra -Wall -g    -c C:\Users\Josh2\Documents\bar\bar.cpp -o obj\Debug\bar.o
    C:\Users\Josh2\Documents\bar\bar.cpp: In function 'int main()':
    C:\Users\Josh2\Documents\bar\bar.cpp:27:28: warning: ISO C++ forbids variable length array 'header' [-Wvla]
    mingw32-g++.exe  -o bin\Debug\bar.exe obj\Debug\bar.o    
    Output size is 960.18 KB
    Process terminated with status 0 (0 minutes, 1 seconds)
    0 errors, 1 warnings

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Why you're calling `getline()` on L36 if you already copied the header text into `header` on L28-L32?

    Anyhow. Your strange behaviour is caused by the fact that `>>` operator skips leading whitespace and extracts up to but not including first whitespace character after valid input. `getline()` has a similar behaviour, but it extracts the delimiting whitespace but doesn't store it.

    After L22, you have a newline character in your input buffer that was not extracted by `>>`. On L36, `getline()` extracts it but doesn't store it into `header`: you never get a chance to enter anything and `header` ends up empty.

    Be careful with `std::string::length()`: it returns the number of characters stored in the string, without the terminating null-character. Any c-style string will need to be `length()` + 1 to properly store contents of `std::string`.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Just use getline(cin, sheader) to read in the header. It's that easy.

    Quote Originally Posted by whiteflags View Post
    Completely unnecessary. getline works with the std::string type. So you might as well just use it and forget the nonsense about copying it to a character array.
    The free function getline works with std::string. cin.getline() does not.
    Last edited by King Mir; 01-28-2013 at 07:34 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by dozerman View Post
    Code:
    sheaderleng=sheader.length();
    char header[sheaderleng];
    this is non-standard C++. G++ appears to compile it without warnings, and other compilers might also allow it, but know that variable length arrays are not guaranteed to be supported. according to the standard, the array size must be a constant expression.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file processing - strange behavior
    By dontoo in forum C Programming
    Replies: 3
    Last Post: 03-17-2010, 08:37 AM
  2. strange std::map behavior
    By manav in forum C++ Programming
    Replies: 63
    Last Post: 04-11-2008, 08:00 AM
  3. return behavior of getline()
    By CodeMonkey in forum C++ Programming
    Replies: 2
    Last Post: 12-19-2006, 11:09 PM
  4. strange behavior
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 10-17-2005, 12:03 PM
  5. odd behavior of cin.getline when it follows cin
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2002, 02:38 PM

Tags for this Thread