1

I want to ask you if is it possible to read the same input (stdin) multiple times? I am about to get really big number, containing thousands of digits (so I am unable to store it in variable, (and also I can not use folders!). My idea is to put the digits into int array, but I don't know how big the array should be, because amount of digits in input may vary. I have to write general solution. So my question is, how to solve it, and how to find out amount of digits (so I can initialize array), before I copy digits into array. I tried using scanf(), multiple times, or scanf() and getchar, but it is not working. See my code:

int main(){
int c;
int amountOfDigits=5;
while(scanf("%1d",&c)!=' '){//finding out number of digits with scanf
    if(isdigit(c)==0){
        break;
    }
    amountOfDigits++;
}

int digits[amountOfDigits];//now i know lenght of array, and initialize it
for(int i=0;i<amountOfDigits;i++){//putting digits into array
    digits[i]=getchar();
}

for(int i=0;i<amountOfDigits;i++){//printing array
    printf("%d",digits[i]);
}
printf("\n");

return 0;
}
victory
  • 185
  • 1
  • 2
  • 16
  • 4
    Allocate some memory. Read with `fgets`. If the input does not end with newline, rellocate the memory and repeat (reading into the previous string end). I am sure there are duplicates. – Weather Vane Mar 29 '18 at 22:13
  • 3
    No, in general you can't reread the input. The usual thing, as Weather Vane mentioned, is to `malloc` and then `realloc` some memory, growing the array bigger and bigger as you read more and more input. Or, on many systems there's a library function `getline` which does this for you automatically. – Steve Summit Mar 29 '18 at 22:18
  • You'll have to use dynamic allocation: malloc() / realloc(). – Lee Daniel Crocker Mar 30 '18 at 01:22
  • However, if you are on Linux, you can use `STDIN_FILENO` and `mmap` the input stream if the information is redirected to your program. – David C. Rankin Mar 30 '18 at 03:26

1 Answers1

2

is it possible to read the same input (stdin) multiple times?

(I am guessing you are a student beginning to learn programming, and you are using Linux; adapt my answer if not)

For your homework, you don't need to read the same input several times. In some cases, it is possible (when the standard input is a genuine file -seekable-, that is when you use some redirection in your command). In other cases (e.g. when the standard input is a pipe, e.g. with a command pipeline; or with here documents in your shell command...) it is not possible to read several times stdin (but you don't need to). In general, don't expect stdin to be seekable with fseek or rewind (it usually is not).

(I am not going to do your homework, but here are useful hints)

so I am unable to store it in variable, (and also I can not use folders!)

You could do several things:

  • (since you mentioned folders....) you might use some more sophisticated ways of storing data on the disk (but in your particular case, I don't recommend that ...). These ways could be some direct-accessed file (ugly), or some indexed file à la gdbm, or some database à la sqlite or even some RDBMS server like PostGreSQL.
    In your case, you don't need any of these; I'm mentioning it since you mentioned "folders" and you meant "directories"!

  • you really should use some heap allocated memory, so read about C dynamic memory allocation and read carefully the documentation of each standard memory management functions like malloc, realloc, free. Your program should probably use all these three functions (don't forget that malloc & realloc could fail).

Read this and that answers. Both are surprisingly relevant.

You probably should keep somehow:

  1. a pointer to heap allocated int-s (actually, you could use char-s)

  2. the allocated size of that pointer

  3. the used length of that thing, that is the actual number of useful digits.

You certainly don't want to grow your array by repeated realloc at each loop (that is inefficient). In practice, you would adapt some growing scheme like newsize = 3*oldsize/2 + 10 to avoid reallocating memory at each step (of your input loop).

you should thank your teacher for a so useful exercise, but you should not expect StackOverflow to do your homework!


Be also aware of arbitrary-precision arithmetic (called bignums or bigints). It is actually hard to code efficiently, so in real-life you would use some library like GMPlib.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Thank you for reply, could you explan why shouldn't I cal calloc too often (in every loop)? I don't want to create more space than it is necessary. – victory Mar 30 '18 at 11:28
  • It would be inefficient. And memory is cheap. You certainly can afford to "lose" 25% of memory. So you should (in practice) create more space than necessary. Of course, when you'll write programs dealing with terabytes of data, your concerns would be very different. BTW don't forget that developer time (the time you spend working on your code) is also costly (and very often more costly that the hardware your program is running on). – Basile Starynkevitch Mar 30 '18 at 11:58