getline with File

This is a discussion on getline with File within the C++ Programming forums, part of the General Programming Boards category; I cant understand why the following code wont work. When it gets to the getline stage, it doesn't wait for ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    22

    getline with File

    I cant understand why the following code wont work. When it gets to the getline stage, it doesn't wait for the prompt.

    I'm running it in Microsoft visual c++ Express and the .h file includes iostream and fstream

    Code:
    #include "file.h"
    using namespace std;
    int main()
    {
    	string file_location;
    	string file_text;
    	cout << "Where would you like to store your file" ;
    	cin >> file_location;
    	cout << endl;
    	cout << "What would you like to write in your file";
    	getline(cin, file_text);
    	ofstream fout(file_location);
    	fout << file_text;
    	fout.close();	
    }
    However, if i change the order around - I.E i move the getline and cin around as the code below demonstrates - it all checks out ok and works.

    Code:
    	string file_location;
    	string file_text;
    	cout << "What would you like to write in your file";
    	getline(cin, file_text);
    	cout << endl;
    	cout << "Where would you like to store your file" ;
    	cin >> file_location;
    	ofstream fout(file_location);
    	fout << file_text;
    	fout.close();

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    cin >> will extract everything up to the first space. The rest gets left behind in the input buffer.
    After that, getline will come and read it all.
    Suggested solution: Use only getline.

    And don't forget to include iostream and string.
    Oh, and you don't need to call close on the streams.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    The >> operator leaves white space AND newlines on the input stream.
    So when the getline comes along (with it's newline only delimiter), it is immediately satisfied by the newline left behind.

    This comes up regularly, search for cin.ignore()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    It is not recommended to use cin >> for anything that has to do with paths (any space would screw up the input).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    22
    Thanks Elysia and Salem.

    What would be best used for a path?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    getline!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a file with getline and skipping lines..
    By kocmohabt33 in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2011, 11:37 PM
  2. Help with getline() and file reading
    By evilkillerfiggi in forum C++ Programming
    Replies: 5
    Last Post: 10-15-2005, 03:49 PM
  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. problem with getline() file I/O
    By hyperion in forum C++ Programming
    Replies: 4
    Last Post: 12-08-2003, 08:34 AM
  5. getline() and file I/O
    By 7stud in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2003, 01:49 PM

Tags for this Thread


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