1

beginner question here, I haven't been able to find examples that relate. I'm working on a C program that will take integer input from stdin using fgets and sscanf, and then write it to an array. However, I'm not sure how to make fgets write to the array.

#define MAXINT 512
char input[MAXINT]

int main(void)
{
    int i;
    int j;
    int count=0;
    int retval;

    while (1==1) {
        fgets(input, MAXINT[count], stdin);
        retval = sscanf(input, "%d", &i);

        if (retval == 1) {
            count = count++;
            }
        else
            if (retval != 1) {
                break;
                }
        }

Would I simply put fgets in a for loop? or is it more complicated than that?

alldavidsluck
  • 77
  • 2
  • 3
  • 10

3 Answers3

2

fgets() reads into a string (array of char), not an array of int.

Your loop should be:

char line[4096];

while (fgets(line, sizeof(line), stdin) != 0)
{
    ...code using sscanf() iteratively to read into the array of int...
}

Not checking inputs leads to problems. At best, your code would process the last line of input twice, most likely. You're only allowed to do that if it means that my refund gets processed twice. At worst, your code might never terminate until your program gets bored to death, or runs out of memory, or you lose patience with it and kill it.

[This] doesn't answer the question of how I would write to the array within the while loop. Would I enclose the sscanf function in a for loop for however many numbers got entered? Would I set something up to run each time Enter is pressed?

Given that you only have one number per line, then the code in the body of the loop is simple:

char line[4096];
int  array[1024];
int  i = 0;

while (fgets(line, sizeof(line), stdin) != 0)
{
    if (i >= 1024)
        break;  // ...too many numbers for array...
    if (sscanf(line, "%d", &array[i++]) != 1)
        ...report error and return/exit...
}

Note that this code won't notice if there is garbage (other numbers, non-numbers) on the same line; it simply grabs the first number (if there is one) and ignores the rest.

If you need multiple numbers per line, look at How to use sscanf() in loops for more information.

If you want a blank line to terminate the input, then using fscanf() or scanf() is not an option; they read through multiple blank lines looking for input.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Maybe `while (fgets(line, sizeof(line), stdin) != NULL)`. – chux - Reinstate Monica Nov 07 '13 at 20:50
  • While I appreciate this, it still doesn't answer the question of how I would write to the array within the while loop. Would I enclose the sscanf function in a for loop for however many numbers got entered? Would I set something up to run each time Enter is pressed? – alldavidsluck Nov 07 '13 at 21:09
  • Short answer: [SO 3975236: How to use `sscanf()` in loops](http://stackoverflow.com/questions/3975236/how-to-use-sscanf-in-loops). I'll add some material here...as work permits. – Jonathan Leffler Nov 07 '13 at 22:07
1

One can put fgets() & ssprintf() in one long condition:

#define MAXINT 512 /* suggest alternate name like Array_Size */
int input[MAXINT];
char buf[100];
int count = 0;

while ((NULL != fgets(buf, sizeof buf, stdin)) && (1 == sscanf(buf,"%d",&input[count]))) {
  if (++count >= MAXINT) break;
}

... or something a bit more user friendly:

// While stdin not closed and input is more than an "Enter" ...
while ((NULL != fgets(buf, sizeof buf, stdin)) && (buf[0] != '\n')) {
  if (1 != sscanf(buf,"%d",&input[count])) {
    puts("Input was not an integer, try again.\n");
    continue;
  }
  if (++count >= MAXINT) break;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

Using sscanf is easy, but put into array ...

C is hard to dynamic init array in terms of size, this sometimes massacre perfectionists.

int main()
{
int total_n;
int n;
int i;
char *s = "yourpattern1yourpattern2yourpattern23";
printf("your string => %s\n", s);
int array[129] = {0};//some big enough number to hold the match
int count = 0;
        while (1 == sscanf(s + total_n, "%*[^0123456789]%d%n", &i, &n))
        {
            total_n += n;
            printf("your match => %d\n", i);
            array[count] = i;
            count++;
        }

printf("your array => \n");
        for (i=0;i<count;i++)
        printf("array[i] => %d\n", array[i]);
}

and the output

[root@localhost ~]# ./a.out
your string => yourpattern1yourpattern2yourpattern23
your match => 1
your match => 2
your match => 23
your array =>
array[i] => 1
array[i] => 2
array[i] => 23
LinconFive
  • 1,718
  • 1
  • 19
  • 24