1

I am trying to read the following input (Variable numbers of integers in a single line which are separated by spaces) through console using C in efficient manner.

Input:

4
3 1 2
6 1 1 1 4
17 2 3 2
12

The number of input lines (here 4, given in first line) are known but number of integers in a line are not known. One of the similar question which I found is given here, and solution is not working for me (I think I have understood this wrong).

while (i < ARRAY_SIZE && scanf("%d", &a[i++] == 1);

I wrote one simple program using this solution:

#include<stdio.h>
int main()
{
int i=0,ARRAY_SIZE=3,a[ARRAY_SIZE];
while (i < ARRAY_SIZE && (scanf("%d", &a[i++]) == 1));
printf ("%d %d %d %d\n",a[0], a[1], a[2], a[3],i);
}

For the above program 2 inputs and corresponding outputs are as follows:

Input-1:

2 3\n
4 5\n  

Output-1:

2 3 4 0 3

Input-2:

1 4 6 7 4 3 3\n 

Output-2:

1 4 6 0 3

But this is not what I was expecting from this program. Here I was trying to read only one line of integers, store them in an array and terminate as soon as one line is over so that If I want to read multiple line I am left with the option of using loop for those many lines (4 in the original input).

It may happen that I am not able to understand above solution and trying to use it at inappropriate place. So guys please help me regarding taking space separated input of integers from console in an efficient fashion.

Community
  • 1
  • 1
Deepak Uniyal
  • 89
  • 3
  • 16
  • Use `gets` and `strtoi/strtol` instead of `scanf`. – i486 Jan 13 '15 at 10:39
  • 1
    No, *never* use `gets`. It is inherently insecure, deprecated and will be removed from the standard library. But consider using `fgets` or `getline` if you like. – 5gon12eder Jan 13 '15 at 10:40

2 Answers2

1

Use

fgets() which read till the end of line and get intgers out of it using strtol() or atoi()

Check the code:

#include <stdio.h>

int main(void) {
    char a[100];
    int b[100];
    int i=0,j;

while(fgets(a, sizeof(a), stdin) != NULL)
{
   char *p = strtok(a, " ");
   while( p != NULL)
   {
     int d = atoi(p); /* Assuming you are entering valid integers else use `strtol()` */
     b[i++] = d;
     p = strtok(NULL, " ");
   }
}

for (j=0;j<i;j++)
printf("%d ",b[j]);
}
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • This is just an example as I am talking ARRAY_SIZE=3 for simplicity so even if I take ARRAY_SIZE=100 there can be more than 100 integers. But my question is not related to array size. Question is related to termination of program when it finds '\n'. – Deepak Uniyal Jan 13 '15 at 10:41
  • is there a guarantee on the total number of integers, or the total number per line.. is `012` in the input ten, twelve, or forbidden. – Jasen Jan 13 '15 at 10:45
  • @Jasen Going by the input shown in the code. This code should fine but have mentioned the alternative also – Gopi Jan 13 '15 at 10:46
  • @Jasen Storing is not important if I could extract all the integers one by one from the given line. Actually my aim to call a function using these numbers. – Deepak Uniyal Jan 13 '15 at 10:49
  • @Gopi There is segmentation fault. – Deepak Uniyal Jan 13 '15 at 10:55
  • @gopi The code you given is pasted here between these lines **int main() { int i=0,a[6]; ##Your code is here ## }** But it's segmentation fault. – Deepak Uniyal Jan 13 '15 at 11:09
  • @DeepakUniyal Sorry I didn't get what you are saying? – Gopi Jan 13 '15 at 11:10
  • I am saying I didn't change anything in your code. The only thing i have wrote extra is int i=0, a[6]; inside main() function. – Deepak Uniyal Jan 13 '15 at 11:14
  • @DeepakUniyal `fgets()` want `char` array so please copy paste my code and run :) :) :) – Gopi Jan 13 '15 at 11:17
  • @Gopi Now I have declared char array as char a[6] and pasted your code but still segmentation fault is there. One thing I have found is that code is running fine just before **int d = atoi(p);** – Deepak Uniyal Jan 13 '15 at 11:28
  • @DeepakUniyal OMG!!! This is not ending. Please follow my words. copy paste the whole code including the headers. The prob is `char a[6]` can't hold a line if your input is like shown above. Increase the buffer size say `char a[100]` – Gopi Jan 13 '15 at 11:30
0

fits the original description for files and line of any length, kind of useless though.

int n,a,s=-1;
fscanf("%d\n",&n);

while(n)
{
  a=getchar();
  if(a==EOF) return 1;
  if(isnumeric(a))
  {  
     if(s==1)
         putchar(' ');
     putchar(a); 
     s=0;
     continue;
  }
  if(!s)
     s=1;
  if(a=='\n') n--;
}
Jasen
  • 11,837
  • 2
  • 30
  • 48