How can I read until the end of an input line?

This is a discussion on How can I read until the end of an input line? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to read in a group of numbers one at a time until I reach the end of the ...

  1. #1
    Registered User Bad_Scooter's Avatar
    Join Date
    Mar 2003
    Posts
    15

    How can I read until the end of an input line?

    I'm trying to read in a group of numbers one at a time until I reach the end of the line from the input file. Something like this:

    999323892...

    I thought that it was possible to do something like this:

    while ( input !='\n')//where input is type char
    {...

    But that doesn't seem to work, no errors, just gets caught in an endless loop. I just can't seem to recall how this should be written, any help would be great.

  2. #2
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    I have attached a file with a simple example... hope that helps!
    Attached Files Attached Files
    Nothing more to tell about me...
    Happy day =)

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Here's probably the easiest way (previous code edited to use std::getline and to make it ANSI-C++ compliant)

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main(){
    
    	std::ifstream file("test.txt");
    	if( file == NULL){
    		std::cerr << "Error opening file << std::endl";
    		return 1;
    	}
    	std::string line;
    	std::getline(file, line);
    
    }
    Now, string will contain the first line of the file (everything until the first newline) so you can do what you want with it. Your best bet is probably to read whole lines in, and then process them later.
    Last edited by Cat; 07-19-2003 at 05:47 PM.

  4. #4
    Registered User Bad_Scooter's Avatar
    Join Date
    Mar 2003
    Posts
    15
    Thanks, works great.

  5. #5
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Wasn´t to read ONE number at a time???
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 03:13 PM
  2. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 11:43 AM
  3. Replies: 3
    Last Post: 04-27-2005, 11:50 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 02:49 PM

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