Thread: Problems Reading Line With Unknown Number of Integers

  1. #1
    Registered User
    Join Date
    Apr 2011

    Problems Reading Line With Unknown Number of Integers

    I need to read a file via stdin (so I'll run my program as ./program <input.txt> output.txt) but I am having problems doing so correctly. The input file starts with an integer x, and x lines follow. Each line starts with an s (for sub-set), followed by an unkown number of integers. So something like this:

    s 1 2 5
    s 3 7 9 11 13
    s 2 4 8 10 12
    s 3
    s 1 5 8 10 13 15 17
    The problem I am facing is to know when to stop, given that each line has an unknown number of integers. I tried this code:

    int main(){
    	int i,x,n;
    	char c;
    	scanf("%c",&c); /*to remove the first line break*/
    	for (i=0;i<x;i++){
    		scanf("%c",&c); /*process the 's'*/
    		while(scanf("%d",&n);){ /*repeat while scanf matches a number*/			
    	return 0;
    But it goes into an infinite loop. Somehow the scanf keeps reading the last number of the first line over and over again. I also tried to read the whole line at once, but then I don't know how to separate the numbers from the 's'.

    Anyone has a different idea?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Is there a known maximum number of integers per line? Also, are you not going to read this into an array? If you are going to read integer by integer, then the number of them really does not matter as you just need to process one at a time, until there are none left.
    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
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    Perhaps scanf() is returning EOF at the end of the line for some reason, which is a non-zero value. Try:

    while(scanf("%d",&n) == 1)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Apr 2011
    That fixed it.

    Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading an unknown number of data points from a table
    By Jonnyfurze in forum C Programming
    Replies: 2
    Last Post: 09-22-2011, 12:23 PM
  2. Calculating Sum of Integers of Unknown Length
    By egnorth in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2010, 04:31 PM
  3. Reading text file by line number
    By whiskedaway in forum C++ Programming
    Replies: 13
    Last Post: 06-16-2009, 10:09 AM
  4. Reading in an unknown number of numbers
    By cboard_member in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2006, 04:21 AM
  5. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 10:31 AM

Tags for this Thread