Problems Reading Line With Unknown Number of Integers

This is a discussion on Problems Reading Line With Unknown Number of Integers within the C Programming forums, part of the General Programming Boards category; I need to read a file via stdin (so I'll run my program as ./program <input.txt> output.txt) but I am ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    33

    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:

    Code:
    5
    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:

    Code:
    int main(){
    	int i,x,n;
    	char c;
    
    	scanf("%d",&x);
    	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*/			
    			printf("%d\n",n);
    		}		
    	}	
    	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
    Location
    Singapore
    Posts
    22,269
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Location
    segmentation fault
    Posts
    8,300
    Perhaps scanf() is returning EOF at the end of the line for some reason, which is a non-zero value. Try:

    Code:
    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
    Posts
    33
    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, 01:23 PM
  2. Calculating Sum of Integers of Unknown Length
    By egnorth in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2010, 05:31 PM
  3. Reading text file by line number
    By whiskedaway in forum C++ Programming
    Replies: 13
    Last Post: 06-16-2009, 11: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, 11:31 AM

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