-2

I want to store a series of integers till i press an enter in an array.How can i implement that

Input: 1(tab space)2(tab space)3(tab space)4(tab space)enter

i tried doing this

#include<stdio.h>
main()
{
int i,j,c,d;
int a[5];
for(i=0;i<2;i++){
        j=0;
    while((d=scanf("%d",&c))==1){
         a[j]=c;
         j=j+1;
    }
 }
}

I dont know how scanf works and using scanf return value.Please explain how i can store this input if its not impossible to do so with scanf and also

2)What else can be used inside scanf along with %d ?

I have a file with 200 rows with numbers like this (NOTE: each row has varied number of values but all numbers are less than 200)

1\t2\t3\t4\t5\t
2\t3\t4\t5\t6\t7\t8\t
11\t12\t13\t
.
.
200

... so i have to store this as an adjacency list representation

sarat
  • 185
  • 9
  • 1 question at a time please – Gopi Dec 21 '14 at 20:25
  • `scanf("%d",...` consumes white-space and does not distinguish between `' ', '\t', '\n'`. To know when a line ends (finding `'\n'`) codes needs another approach. – chux - Reinstate Monica Dec 21 '14 at 20:38
  • Follow the link for the POSIX view on what you can use with [`scanf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/scanf.html). Be aware that using `scanf()` accurately is surprisingly hard, at least once you get beyond reading integers. In your code, the assignment to `d` is unnecessary; the variable is never referenced. It would make sense if there was error recovery code that needed to know how many of the values were read successfully (because `scanf()` can read multiple values with a single call). Consider using `" %c"` instead of `"%c"` when reading characters. – Jonathan Leffler Dec 21 '14 at 20:42
  • @chux What would be that approach? and also in the above comment you were saying scanf("%d",..)consumes white-space how is scanf(" %d") different? – sarat Dec 21 '14 at 21:06
  • @sarat: note that `scanf()` doesn't pay attention to newlines for most practical purposes. With terminal input, the program will see nothing until you hit return, but `scanf()` will merrily read past newlines looking for more input. If you want to do line-based input, use [`fgets()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html) or [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html), and then [`sscanf()` in a loop](http://stackoverflow.com/questions/3975236/how-to-use-sscanf-in-loops/3975254#3975254.) – Jonathan Leffler Dec 21 '14 at 21:36
  • @sarat `scanf("%d",..` and `scanf(" %d",...` are effectively the same. – chux - Reinstate Monica Dec 21 '14 at 23:10

2 Answers2

1

For the first part of your question. scanf() returns number of elements successfully read but it is of no use here and you can just scan in a loop and scanf() will pick your integers in a line when you press enter.

   #include <stdio.h>

int main(void) {
    int a[5];
    int i, n;
    for(i=0;i<5;i++)
    {
       if(scanf("%d",&a[i]) != 1)
       {
          printf("Value not read correctly\n");
          break;
       }
    }
    n = i;
    for(i=0;i<n;i++)
        printf("%d\n",a[i]);
    return 0;
}

For the second question you have to do something line

1.Read a line from your file using fgets()

2.Break your line using strtok() with tab as delimiter.

3.Now convert each token to integer using atoi()

4.Now do whatever you want with the integer. i.e. create a node add your integer to the node

Gopi
  • 19,784
  • 4
  • 24
  • 36
0

Let's make some reasonable assumptions about the width of each row.
These assumptions are useful for simple code, though not needed in general.

#define LINE_WIDTH_MAX 1000
#define INTS_PER_LINE_MAX 100
#define ROWS_PER_FILE (200 /* given by OP */)

Read each row with fgets(), then scan. Could use strtol(), sscanf() or various approaches.

This method uses sscanf() and "%n" to determine when the next number might follow.

int row;
for (row = 0; row < ROWS_PER_FILE; row++) {
  char buf[LINE_WIDTH_MAX + 2];
  if (fgets(buf, sizeof buf, stdin) == NULL) {
    break;  // Handle EOF or IO error
  }
  int num[INTS_PER_LINE_MAX];
  char *p = buf;
  for (int i = 0; i<INTS_PER_LINE_MAX; i++) {
    int n = 0;
    if (1 != sscanf(p, "%d %n", &num[i], &n)) {
      break;
    }
    p += n;
  }
  if (*p) Handle_GarbageInLIne();

  // do something with the `i` numbers

}

Notes:

Advise never use scanf()/

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256