Thread: multiple lines in a string

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149

    multiple lines in a string

    I created a program that accepts input from the keyboard and stores it in a string. Then it outputs the string in a file. This works, and is as follows:
    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string sentence;
    	ofstream file;
    	char filename[100];	
    
    	cout << "Enter text to be written to file: ";
    	getline(cin, sentence);
    	cout << endl;
    	cout << "The text will be written to the current directory (contains the .cpp file)." << endl;
    	cout << "The file will be called Output.txt" << endl;	
    	file.open("Output.txt", ios::out);
    	file << sentence;	
    	
    	
    	file.close();
    	cout << endl;
    	cin.get();
    	cout << endl;
    	return 0;
    }
    The problem:
    In unix, I have to redirect the stdin to a file (a.out < file.txt). So, I want this to read all the contents of "file.txt" into the string. However, only the first line of the file is being read into string. I think it probably has something to do with getline, but i'm not sure how to fix it. I thought getline is suppose to include all whitespace, but maybe it has something to do with unix (\n or endl?).
    By the way, I'm trying to use a string instead of a char.

    This is an example of the file:
    Code:
    This is sample text that needs
    to be read correctly. More sample
    text is as follows.
    Only the first line reads into the string.


    Thanks.
    IDE - Visual Studio 2005
    Windows XP Pro

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, getline gets a line (unintuitive name, I know). If you want everything, then you can call getline multiple times (until a read fails).

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    getline allows you to choose a delimiter (the default is newline).
    Pick a character which isn't in the file, and you should get the whole file in your string.
    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.

  4. #4
    Banal internet user
    Join Date
    Aug 2002
    Posts
    1,380
    Quote Originally Posted by Cpro View Post
    I think it probably has something to do with getline, but i'm not sure how to fix it. I thought getline is suppose to include all whitespace, but maybe it has something to do with unix (\n or endl?)
    getline will copy up until it reads a newline character and then stops... hence the name getline.
    Last edited by BMJ; 06-11-2008 at 02:07 PM.

  5. #5
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Ok, i thought getline read multiple lines.
    Anyways, what would be the best way to read all the text into the string, without haveing to output each line before the next. For exaple, i tried
    Code:
    while(1)
    	{		
    		getline(cin, sentence);
    		if(getline(cin, sentence).fail()) break;
    	}
    This will overwrite the string everytime a new line is encountered. So, i would have to output each line before it is over-written. However, I would like to read all the lines into the string, so it's one long string that can be output at the end of the program (maybe this is not possible?).
    Also, if i use this with the keyboard (instead of file) as input, the while loop will never break. It does with the file.

    Thanks.
    IDE - Visual Studio 2005
    Windows XP Pro

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Use cin.rdbuf():

    Code:
            string_stream writeMe;
            cout << "Enter text to be written to file: ";
    	file.open("Output.txt", ios::out);
            file << cin.rdbuf();
    	cout << "The text will be written to the current directory (contains the .cpp file)." << endl;
    	cout << "The file will be called Output.txt" << endl;
    To read into a string, read it into a ostringstream then use the str method to get the string, or rdbuf to get the buffer.
    Last edited by King Mir; 06-11-2008 at 02:21 PM.
    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.

  7. #7
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    Ok, i got it working now. Thanks.
    One more question.

    I have to make a program that takes names from input (the keyboard) and outputs them in reverse order (to the screen). This works, and is as follows:
    Code:
    int main()
    {
    	string name;
    	string tempName;
    	string nameArray[5];
    	int nameSize;
    
    	
    	//cout << "Enter five names: " << endl;
    	for(int i = 0; i < 5; i++)
    	{
    		cin >> name;
    		nameArray[i] = name;
    	}
    
    	cout << endl;
    	
    	//cout << "The names are: " << endl;
    	for(int j = 0; j < 5; j++)
    	{
    		tempName = nameArray[j];
    		nameSize = tempName.size()-1;
    
    		while(nameSize >= 0)
    		{
    			cout << tempName[nameSize];
    			nameSize--;
    		}		
    		cout << endl;
    	}
    
    	cout << endl;
    	cin.get();
    	cout << endl;
    	return 0;
    }
    The problem:
    I have to use unix commands to direct input and output to and from files (ex: a.out > file1.txt). Is there a way to ouput only the names to the file, instead of the prompts ("Enter five names: ")? Because, when i direct the output, it will print all the cout statements to the file. Then when i direct that file as input (a.out < file1.txt) the prompt will be read into a name. So, i'm trying to figure out a way to only print the names to the output file, while still prompting the user on the screen.

    Thanks.
    IDE - Visual Studio 2005
    Windows XP Pro

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you write to std::cerr instead of std::cout, that output won't get redirected. (Yes, it's a bit of an abuse, since cerr is supposed to be the error console, but whatever.) OTOH: If you're planning to redirect input as well, why print the prompt? (And note that you won't be able to tell, programmatically, that std::cin is all of a sudden infile.txt.)

  9. #9
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    That works. Thanks.

    "OTOH: If you're planning to redirect input as well, why print the prompt? "

    The program has to work both ways (using the screen and a file for input).


    Thanks.
    IDE - Visual Studio 2005
    Windows XP Pro

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I'd recommend using clog instead of cerr. It's a little faster since the output is buffered, but otherwise the behavior is the same.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM