3

I'm trying to get numbers from stdin to an array. the first number in stdin is the number of elements in the array (the number can be any int). I did this to get the first number:

while(c=getchar()!=' '){
n*=10;
n+=atoi(c);
}

And then created an array of size n. Now I need to go through all the rest

 while(c=getchar()!=EOF)

and add numbers to the array. The numbers are separated by \t and sometimes \n as well. How would I do that? I've been thinking for an hour and still haven't got a working code. Any help? Thanks!

MinaHany
  • 1,015
  • 4
  • 16
  • 32

1 Answers1

3

Unless you're feeling particularly masochistic (or can't due to homework requirements), you'd normally do it using scanf:

int n;
int *numbers;

scanf("%d", &n);

numbers = malloc(n * sizeof(*numbers));

for (int i=0; i<n; i++)
    scanf("%d", &numbers[i]);

For more robust error handling, you frequently want to read a line at a time using fgets, then parse that into individual numbers using sscanf (or something similar).

As an aside: no you should not cast the return from malloc to int *. It's neither necessary nor desirable in C. Just #include <stdlib.h>, and assign the result as shown above. If your compiler is giving you a warning (or error) about a conversion, that means one of two things is happening: either you've forgotten to #include <stdlib.h> as required, or else you're actually compiling as C++. If you're writing C++, write real C++, which means you shouldn't be using malloc at all.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • WOW, I didn't know I could do that! Can you please explain however, why is anything between the numbers ignored? And how the second scanf continues from where the first scanf stopped? – MinaHany Apr 08 '12 at 07:45
  • The "%d" scanf conversion skips across white-space before a number (and tabs and new-lines are both considered white-space). When you open a file, it has a "current position" indicator, which is updated by the functions that read from it (`getc`, `scanf`, `fread`, etc.) – Jerry Coffin Apr 08 '12 at 07:47
  • but in the case of %c a white space is counted correct? Ahh yes, I know about the current position but didn't know that scanf uses it as well. – MinaHany Apr 08 '12 at 07:51
  • @MinaHany: Yes, %c will read spaces. In fact, the fundamental difference between %c and %s is that %s skips leading white space, then reads up to the next white space, where %c just reads in as many characters as you specify. – Jerry Coffin Apr 08 '12 at 07:55
  • So if on stdin was "StringA StringB StringC" then scanf %s will automatically read 3 strings and neglect spaces but scanf %c will read chars one by one. Thank you so much mate! Have a nice day :) – MinaHany Apr 08 '12 at 08:02
  • @MinaHany: `%c` will read one by one, but you can also specify a count, so `%26c` would read all of "StringA StringB StringC" at once (at least if I've counted correctly). – Jerry Coffin Apr 08 '12 at 08:03
  • Thanks a lot for the extra info! Cheers mate – MinaHany Apr 08 '12 at 08:11