How read two lines numbers from input to 2 arrays, C language? Without knowing the amount of number? in C
up vote
0
down vote
favorite
For example, input is:
12 23 32 41 45
22 11 43
lines end with 'n'
,
I want save nums to a
and b
;
a = {12, 23, 32, 41, 45}
b = {22, 11, 43}
The point is I DON'T KNOW how many number of each line.
If I know line1 n
numbers and line2 m
numbers, I will use "for loop", and no question.
Just like,
for(int i = 0; i < n; i++) scanf("%d", a+i);
for(int i = 0; i < m; i++) scanf("%d", b+i);
But, I DO NOT know n and m, How to do, guys?
c scanf
|
show 1 more comment
up vote
0
down vote
favorite
For example, input is:
12 23 32 41 45
22 11 43
lines end with 'n'
,
I want save nums to a
and b
;
a = {12, 23, 32, 41, 45}
b = {22, 11, 43}
The point is I DON'T KNOW how many number of each line.
If I know line1 n
numbers and line2 m
numbers, I will use "for loop", and no question.
Just like,
for(int i = 0; i < n; i++) scanf("%d", a+i);
for(int i = 0; i < m; i++) scanf("%d", b+i);
But, I DO NOT know n and m, How to do, guys?
c scanf
Can you accept a number max for array length ?
– Ôrel
Nov 19 at 17:03
1
If you care about line endings, then you either need to read one character at a time withfgetc
, or read a line at a time withfgets
.
– user3386109
Nov 19 at 17:06
Read characters until a new line is reached, if one isn't reached, resize your buffer? You'll want to use dynamic memory allocation probably.
– jacob
Nov 19 at 17:07
2
A good function for reading a line at a time isgetline
, which was standardized in POSIX.1-2008. In comparision tofgets
, it can read a line of arbitrary length into a dynamically allocated buffer. If you don't have thegetline
function, there are various standalone implementations of it, such as the one at github.com/ivanrad/getline .
– Ian Abbott
Nov 19 at 17:19
You could use awhile
loop and some arbitrary input as break condition & dynamically alloc your input array(i think).
– ats
Nov 19 at 17:25
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
For example, input is:
12 23 32 41 45
22 11 43
lines end with 'n'
,
I want save nums to a
and b
;
a = {12, 23, 32, 41, 45}
b = {22, 11, 43}
The point is I DON'T KNOW how many number of each line.
If I know line1 n
numbers and line2 m
numbers, I will use "for loop", and no question.
Just like,
for(int i = 0; i < n; i++) scanf("%d", a+i);
for(int i = 0; i < m; i++) scanf("%d", b+i);
But, I DO NOT know n and m, How to do, guys?
c scanf
For example, input is:
12 23 32 41 45
22 11 43
lines end with 'n'
,
I want save nums to a
and b
;
a = {12, 23, 32, 41, 45}
b = {22, 11, 43}
The point is I DON'T KNOW how many number of each line.
If I know line1 n
numbers and line2 m
numbers, I will use "for loop", and no question.
Just like,
for(int i = 0; i < n; i++) scanf("%d", a+i);
for(int i = 0; i < m; i++) scanf("%d", b+i);
But, I DO NOT know n and m, How to do, guys?
c scanf
c scanf
edited Nov 19 at 19:54
ReAl
560216
560216
asked Nov 19 at 16:58
user7331489
1
1
Can you accept a number max for array length ?
– Ôrel
Nov 19 at 17:03
1
If you care about line endings, then you either need to read one character at a time withfgetc
, or read a line at a time withfgets
.
– user3386109
Nov 19 at 17:06
Read characters until a new line is reached, if one isn't reached, resize your buffer? You'll want to use dynamic memory allocation probably.
– jacob
Nov 19 at 17:07
2
A good function for reading a line at a time isgetline
, which was standardized in POSIX.1-2008. In comparision tofgets
, it can read a line of arbitrary length into a dynamically allocated buffer. If you don't have thegetline
function, there are various standalone implementations of it, such as the one at github.com/ivanrad/getline .
– Ian Abbott
Nov 19 at 17:19
You could use awhile
loop and some arbitrary input as break condition & dynamically alloc your input array(i think).
– ats
Nov 19 at 17:25
|
show 1 more comment
Can you accept a number max for array length ?
– Ôrel
Nov 19 at 17:03
1
If you care about line endings, then you either need to read one character at a time withfgetc
, or read a line at a time withfgets
.
– user3386109
Nov 19 at 17:06
Read characters until a new line is reached, if one isn't reached, resize your buffer? You'll want to use dynamic memory allocation probably.
– jacob
Nov 19 at 17:07
2
A good function for reading a line at a time isgetline
, which was standardized in POSIX.1-2008. In comparision tofgets
, it can read a line of arbitrary length into a dynamically allocated buffer. If you don't have thegetline
function, there are various standalone implementations of it, such as the one at github.com/ivanrad/getline .
– Ian Abbott
Nov 19 at 17:19
You could use awhile
loop and some arbitrary input as break condition & dynamically alloc your input array(i think).
– ats
Nov 19 at 17:25
Can you accept a number max for array length ?
– Ôrel
Nov 19 at 17:03
Can you accept a number max for array length ?
– Ôrel
Nov 19 at 17:03
1
1
If you care about line endings, then you either need to read one character at a time with
fgetc
, or read a line at a time with fgets
.– user3386109
Nov 19 at 17:06
If you care about line endings, then you either need to read one character at a time with
fgetc
, or read a line at a time with fgets
.– user3386109
Nov 19 at 17:06
Read characters until a new line is reached, if one isn't reached, resize your buffer? You'll want to use dynamic memory allocation probably.
– jacob
Nov 19 at 17:07
Read characters until a new line is reached, if one isn't reached, resize your buffer? You'll want to use dynamic memory allocation probably.
– jacob
Nov 19 at 17:07
2
2
A good function for reading a line at a time is
getline
, which was standardized in POSIX.1-2008. In comparision to fgets
, it can read a line of arbitrary length into a dynamically allocated buffer. If you don't have the getline
function, there are various standalone implementations of it, such as the one at github.com/ivanrad/getline .– Ian Abbott
Nov 19 at 17:19
A good function for reading a line at a time is
getline
, which was standardized in POSIX.1-2008. In comparision to fgets
, it can read a line of arbitrary length into a dynamically allocated buffer. If you don't have the getline
function, there are various standalone implementations of it, such as the one at github.com/ivanrad/getline .– Ian Abbott
Nov 19 at 17:19
You could use a
while
loop and some arbitrary input as break condition & dynamically alloc your input array(i think).– ats
Nov 19 at 17:25
You could use a
while
loop and some arbitrary input as break condition & dynamically alloc your input array(i think).– ats
Nov 19 at 17:25
|
show 1 more comment
3 Answers
3
active
oldest
votes
up vote
1
down vote
If you would like to continue using a scanf approach, I would recommend the negated scanset %[^]
format specifier.
scanf("%[^n]", pointerToCharArray)
This should read in any number of characters up to but not including the character specified (which, in our case, is a newline). If you'd like to discard the newline, read it in as follows:
scanf("%[^n]n", pointerToCharArray)
A link to the reference page can be found below. The negated scanset specifier is included in the list:
http://www.cplusplus.com/reference/cstdio/scanf/
From this point, it is a simple matter to use strtok() to tokenize the output of the scanf into number arrays. If you are not familiar with strtok, another reference link is provided below:
http://www.cplusplus.com/reference/cstring/strtok/
I hope this helps!
scanf("%[^n]n", pointerToCharArray)
not only consumes a following newline, it waits for additional input consuming all other white-spaces.scanf()
then does not return until following non-whitespace detected or EOF.
– chux
Nov 19 at 18:57
As OP is concerned about "I DON'T KNOW how many number ", using"%[^n]"
without a width limit is fragile code.
– chux
Nov 19 at 18:58
Thank you very much!
– user7331489
Nov 22 at 8:43
add a comment |
up vote
0
down vote
Let us use the non standard getline
, a common library extension. @Abbott to read the entire line into allocated memory.
If an array is needed and its size determined at run-time, use a variable length array available in C99 and optionally in C11.
Make a function to count and maybe save the numbers. Use strto...()
functions for robust parsing.
size_t count_longs(char *line, long *dest) {
size_t count = 0;
char *s = line;
for (;;) {
char *endptr;
long num = strtol(s, &endptr, 10);
if (s == endptr) {
break; /// no conversion
}
s = endptr;
if (dest) {
dest[count] = num;
}
count++;
}
return count;
}
Sample code snippet
char *line = NULL;
size_t len = 0;
ssize_t nread = getline(&line, &len, stdin); // read the whole line
if (nread != -1) {
// successful read
long a[count_longs(line, NULL)]; // determine array size and use a
count_longs(line, a); // 2nd pass: assign array elements.
nread = getline(&line, &len, stdin);
if (nread != -1) {
// successful read
long b[count_longs(line, NULL)];
count_longs(line, b);
// Do something with a and b
}
}
free(line);
len = 0;
add a comment |
up vote
0
down vote
A solution that could potentially fit what OP asked for would be the code below.
The solution reads an unspecified number of integer values & stores them in arr
. to break the loop/end input simply input a non-integer value. This could be further specified to search for a specific input etc.
int c = 0;
int *arr = NULL;
int *extArr = NULL;
int i = 0;
for (i = 0; scanf("%d", &c) == 1; i++)
{
extArr = (int*)realloc(arr, (i+1) * sizeof(int));
arr = extArr;
arr[i] = c;
}
free(arr);
I would, however personally go with a mix between the solution i provided & armitus answer(if i cant use getline
or similar) since reallocating memory for every single int value seems redundant to me.
Edit Since there has been some question about the code i provided and how to best use it in order to fulfill the OP's question. The codesnippet was supposed to showcase how one might create his own scanf-style function for unspecified number of integer inputs reading one full array at a time(in that case the free()
has to be omitted till one is done with the array(obviously).
I do see a functional problem.scanf("%d", &c)
will read through an end-of-line like any other white-space. I'd expect OP's two lines of integers to end up in onearr
here.
– chux
Nov 19 at 19:02
The idea was to use my codesnippet as a standalone function & then just call it n times for the number of array's required. i admittedly omitted those details from my answer.
– ats
Nov 19 at 19:06
Trouble is we do not known
prior to reading the line.
– chux
Nov 19 at 19:08
Maybe i should've phrased that better. I did not mean n as in array length, but n as in number of array's read. Since the codesnippet can easily be packed inside a function and used as a sort of scanf-overload for array's
– ats
Nov 19 at 19:10
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
If you would like to continue using a scanf approach, I would recommend the negated scanset %[^]
format specifier.
scanf("%[^n]", pointerToCharArray)
This should read in any number of characters up to but not including the character specified (which, in our case, is a newline). If you'd like to discard the newline, read it in as follows:
scanf("%[^n]n", pointerToCharArray)
A link to the reference page can be found below. The negated scanset specifier is included in the list:
http://www.cplusplus.com/reference/cstdio/scanf/
From this point, it is a simple matter to use strtok() to tokenize the output of the scanf into number arrays. If you are not familiar with strtok, another reference link is provided below:
http://www.cplusplus.com/reference/cstring/strtok/
I hope this helps!
scanf("%[^n]n", pointerToCharArray)
not only consumes a following newline, it waits for additional input consuming all other white-spaces.scanf()
then does not return until following non-whitespace detected or EOF.
– chux
Nov 19 at 18:57
As OP is concerned about "I DON'T KNOW how many number ", using"%[^n]"
without a width limit is fragile code.
– chux
Nov 19 at 18:58
Thank you very much!
– user7331489
Nov 22 at 8:43
add a comment |
up vote
1
down vote
If you would like to continue using a scanf approach, I would recommend the negated scanset %[^]
format specifier.
scanf("%[^n]", pointerToCharArray)
This should read in any number of characters up to but not including the character specified (which, in our case, is a newline). If you'd like to discard the newline, read it in as follows:
scanf("%[^n]n", pointerToCharArray)
A link to the reference page can be found below. The negated scanset specifier is included in the list:
http://www.cplusplus.com/reference/cstdio/scanf/
From this point, it is a simple matter to use strtok() to tokenize the output of the scanf into number arrays. If you are not familiar with strtok, another reference link is provided below:
http://www.cplusplus.com/reference/cstring/strtok/
I hope this helps!
scanf("%[^n]n", pointerToCharArray)
not only consumes a following newline, it waits for additional input consuming all other white-spaces.scanf()
then does not return until following non-whitespace detected or EOF.
– chux
Nov 19 at 18:57
As OP is concerned about "I DON'T KNOW how many number ", using"%[^n]"
without a width limit is fragile code.
– chux
Nov 19 at 18:58
Thank you very much!
– user7331489
Nov 22 at 8:43
add a comment |
up vote
1
down vote
up vote
1
down vote
If you would like to continue using a scanf approach, I would recommend the negated scanset %[^]
format specifier.
scanf("%[^n]", pointerToCharArray)
This should read in any number of characters up to but not including the character specified (which, in our case, is a newline). If you'd like to discard the newline, read it in as follows:
scanf("%[^n]n", pointerToCharArray)
A link to the reference page can be found below. The negated scanset specifier is included in the list:
http://www.cplusplus.com/reference/cstdio/scanf/
From this point, it is a simple matter to use strtok() to tokenize the output of the scanf into number arrays. If you are not familiar with strtok, another reference link is provided below:
http://www.cplusplus.com/reference/cstring/strtok/
I hope this helps!
If you would like to continue using a scanf approach, I would recommend the negated scanset %[^]
format specifier.
scanf("%[^n]", pointerToCharArray)
This should read in any number of characters up to but not including the character specified (which, in our case, is a newline). If you'd like to discard the newline, read it in as follows:
scanf("%[^n]n", pointerToCharArray)
A link to the reference page can be found below. The negated scanset specifier is included in the list:
http://www.cplusplus.com/reference/cstdio/scanf/
From this point, it is a simple matter to use strtok() to tokenize the output of the scanf into number arrays. If you are not familiar with strtok, another reference link is provided below:
http://www.cplusplus.com/reference/cstring/strtok/
I hope this helps!
answered Nov 19 at 17:51
armitus
695
695
scanf("%[^n]n", pointerToCharArray)
not only consumes a following newline, it waits for additional input consuming all other white-spaces.scanf()
then does not return until following non-whitespace detected or EOF.
– chux
Nov 19 at 18:57
As OP is concerned about "I DON'T KNOW how many number ", using"%[^n]"
without a width limit is fragile code.
– chux
Nov 19 at 18:58
Thank you very much!
– user7331489
Nov 22 at 8:43
add a comment |
scanf("%[^n]n", pointerToCharArray)
not only consumes a following newline, it waits for additional input consuming all other white-spaces.scanf()
then does not return until following non-whitespace detected or EOF.
– chux
Nov 19 at 18:57
As OP is concerned about "I DON'T KNOW how many number ", using"%[^n]"
without a width limit is fragile code.
– chux
Nov 19 at 18:58
Thank you very much!
– user7331489
Nov 22 at 8:43
scanf("%[^n]n", pointerToCharArray)
not only consumes a following newline, it waits for additional input consuming all other white-spaces. scanf()
then does not return until following non-whitespace detected or EOF.– chux
Nov 19 at 18:57
scanf("%[^n]n", pointerToCharArray)
not only consumes a following newline, it waits for additional input consuming all other white-spaces. scanf()
then does not return until following non-whitespace detected or EOF.– chux
Nov 19 at 18:57
As OP is concerned about "I DON'T KNOW how many number ", using
"%[^n]"
without a width limit is fragile code.– chux
Nov 19 at 18:58
As OP is concerned about "I DON'T KNOW how many number ", using
"%[^n]"
without a width limit is fragile code.– chux
Nov 19 at 18:58
Thank you very much!
– user7331489
Nov 22 at 8:43
Thank you very much!
– user7331489
Nov 22 at 8:43
add a comment |
up vote
0
down vote
Let us use the non standard getline
, a common library extension. @Abbott to read the entire line into allocated memory.
If an array is needed and its size determined at run-time, use a variable length array available in C99 and optionally in C11.
Make a function to count and maybe save the numbers. Use strto...()
functions for robust parsing.
size_t count_longs(char *line, long *dest) {
size_t count = 0;
char *s = line;
for (;;) {
char *endptr;
long num = strtol(s, &endptr, 10);
if (s == endptr) {
break; /// no conversion
}
s = endptr;
if (dest) {
dest[count] = num;
}
count++;
}
return count;
}
Sample code snippet
char *line = NULL;
size_t len = 0;
ssize_t nread = getline(&line, &len, stdin); // read the whole line
if (nread != -1) {
// successful read
long a[count_longs(line, NULL)]; // determine array size and use a
count_longs(line, a); // 2nd pass: assign array elements.
nread = getline(&line, &len, stdin);
if (nread != -1) {
// successful read
long b[count_longs(line, NULL)];
count_longs(line, b);
// Do something with a and b
}
}
free(line);
len = 0;
add a comment |
up vote
0
down vote
Let us use the non standard getline
, a common library extension. @Abbott to read the entire line into allocated memory.
If an array is needed and its size determined at run-time, use a variable length array available in C99 and optionally in C11.
Make a function to count and maybe save the numbers. Use strto...()
functions for robust parsing.
size_t count_longs(char *line, long *dest) {
size_t count = 0;
char *s = line;
for (;;) {
char *endptr;
long num = strtol(s, &endptr, 10);
if (s == endptr) {
break; /// no conversion
}
s = endptr;
if (dest) {
dest[count] = num;
}
count++;
}
return count;
}
Sample code snippet
char *line = NULL;
size_t len = 0;
ssize_t nread = getline(&line, &len, stdin); // read the whole line
if (nread != -1) {
// successful read
long a[count_longs(line, NULL)]; // determine array size and use a
count_longs(line, a); // 2nd pass: assign array elements.
nread = getline(&line, &len, stdin);
if (nread != -1) {
// successful read
long b[count_longs(line, NULL)];
count_longs(line, b);
// Do something with a and b
}
}
free(line);
len = 0;
add a comment |
up vote
0
down vote
up vote
0
down vote
Let us use the non standard getline
, a common library extension. @Abbott to read the entire line into allocated memory.
If an array is needed and its size determined at run-time, use a variable length array available in C99 and optionally in C11.
Make a function to count and maybe save the numbers. Use strto...()
functions for robust parsing.
size_t count_longs(char *line, long *dest) {
size_t count = 0;
char *s = line;
for (;;) {
char *endptr;
long num = strtol(s, &endptr, 10);
if (s == endptr) {
break; /// no conversion
}
s = endptr;
if (dest) {
dest[count] = num;
}
count++;
}
return count;
}
Sample code snippet
char *line = NULL;
size_t len = 0;
ssize_t nread = getline(&line, &len, stdin); // read the whole line
if (nread != -1) {
// successful read
long a[count_longs(line, NULL)]; // determine array size and use a
count_longs(line, a); // 2nd pass: assign array elements.
nread = getline(&line, &len, stdin);
if (nread != -1) {
// successful read
long b[count_longs(line, NULL)];
count_longs(line, b);
// Do something with a and b
}
}
free(line);
len = 0;
Let us use the non standard getline
, a common library extension. @Abbott to read the entire line into allocated memory.
If an array is needed and its size determined at run-time, use a variable length array available in C99 and optionally in C11.
Make a function to count and maybe save the numbers. Use strto...()
functions for robust parsing.
size_t count_longs(char *line, long *dest) {
size_t count = 0;
char *s = line;
for (;;) {
char *endptr;
long num = strtol(s, &endptr, 10);
if (s == endptr) {
break; /// no conversion
}
s = endptr;
if (dest) {
dest[count] = num;
}
count++;
}
return count;
}
Sample code snippet
char *line = NULL;
size_t len = 0;
ssize_t nread = getline(&line, &len, stdin); // read the whole line
if (nread != -1) {
// successful read
long a[count_longs(line, NULL)]; // determine array size and use a
count_longs(line, a); // 2nd pass: assign array elements.
nread = getline(&line, &len, stdin);
if (nread != -1) {
// successful read
long b[count_longs(line, NULL)];
count_longs(line, b);
// Do something with a and b
}
}
free(line);
len = 0;
edited Nov 19 at 18:19
answered Nov 19 at 18:05
chux
78.8k869145
78.8k869145
add a comment |
add a comment |
up vote
0
down vote
A solution that could potentially fit what OP asked for would be the code below.
The solution reads an unspecified number of integer values & stores them in arr
. to break the loop/end input simply input a non-integer value. This could be further specified to search for a specific input etc.
int c = 0;
int *arr = NULL;
int *extArr = NULL;
int i = 0;
for (i = 0; scanf("%d", &c) == 1; i++)
{
extArr = (int*)realloc(arr, (i+1) * sizeof(int));
arr = extArr;
arr[i] = c;
}
free(arr);
I would, however personally go with a mix between the solution i provided & armitus answer(if i cant use getline
or similar) since reallocating memory for every single int value seems redundant to me.
Edit Since there has been some question about the code i provided and how to best use it in order to fulfill the OP's question. The codesnippet was supposed to showcase how one might create his own scanf-style function for unspecified number of integer inputs reading one full array at a time(in that case the free()
has to be omitted till one is done with the array(obviously).
I do see a functional problem.scanf("%d", &c)
will read through an end-of-line like any other white-space. I'd expect OP's two lines of integers to end up in onearr
here.
– chux
Nov 19 at 19:02
The idea was to use my codesnippet as a standalone function & then just call it n times for the number of array's required. i admittedly omitted those details from my answer.
– ats
Nov 19 at 19:06
Trouble is we do not known
prior to reading the line.
– chux
Nov 19 at 19:08
Maybe i should've phrased that better. I did not mean n as in array length, but n as in number of array's read. Since the codesnippet can easily be packed inside a function and used as a sort of scanf-overload for array's
– ats
Nov 19 at 19:10
add a comment |
up vote
0
down vote
A solution that could potentially fit what OP asked for would be the code below.
The solution reads an unspecified number of integer values & stores them in arr
. to break the loop/end input simply input a non-integer value. This could be further specified to search for a specific input etc.
int c = 0;
int *arr = NULL;
int *extArr = NULL;
int i = 0;
for (i = 0; scanf("%d", &c) == 1; i++)
{
extArr = (int*)realloc(arr, (i+1) * sizeof(int));
arr = extArr;
arr[i] = c;
}
free(arr);
I would, however personally go with a mix between the solution i provided & armitus answer(if i cant use getline
or similar) since reallocating memory for every single int value seems redundant to me.
Edit Since there has been some question about the code i provided and how to best use it in order to fulfill the OP's question. The codesnippet was supposed to showcase how one might create his own scanf-style function for unspecified number of integer inputs reading one full array at a time(in that case the free()
has to be omitted till one is done with the array(obviously).
I do see a functional problem.scanf("%d", &c)
will read through an end-of-line like any other white-space. I'd expect OP's two lines of integers to end up in onearr
here.
– chux
Nov 19 at 19:02
The idea was to use my codesnippet as a standalone function & then just call it n times for the number of array's required. i admittedly omitted those details from my answer.
– ats
Nov 19 at 19:06
Trouble is we do not known
prior to reading the line.
– chux
Nov 19 at 19:08
Maybe i should've phrased that better. I did not mean n as in array length, but n as in number of array's read. Since the codesnippet can easily be packed inside a function and used as a sort of scanf-overload for array's
– ats
Nov 19 at 19:10
add a comment |
up vote
0
down vote
up vote
0
down vote
A solution that could potentially fit what OP asked for would be the code below.
The solution reads an unspecified number of integer values & stores them in arr
. to break the loop/end input simply input a non-integer value. This could be further specified to search for a specific input etc.
int c = 0;
int *arr = NULL;
int *extArr = NULL;
int i = 0;
for (i = 0; scanf("%d", &c) == 1; i++)
{
extArr = (int*)realloc(arr, (i+1) * sizeof(int));
arr = extArr;
arr[i] = c;
}
free(arr);
I would, however personally go with a mix between the solution i provided & armitus answer(if i cant use getline
or similar) since reallocating memory for every single int value seems redundant to me.
Edit Since there has been some question about the code i provided and how to best use it in order to fulfill the OP's question. The codesnippet was supposed to showcase how one might create his own scanf-style function for unspecified number of integer inputs reading one full array at a time(in that case the free()
has to be omitted till one is done with the array(obviously).
A solution that could potentially fit what OP asked for would be the code below.
The solution reads an unspecified number of integer values & stores them in arr
. to break the loop/end input simply input a non-integer value. This could be further specified to search for a specific input etc.
int c = 0;
int *arr = NULL;
int *extArr = NULL;
int i = 0;
for (i = 0; scanf("%d", &c) == 1; i++)
{
extArr = (int*)realloc(arr, (i+1) * sizeof(int));
arr = extArr;
arr[i] = c;
}
free(arr);
I would, however personally go with a mix between the solution i provided & armitus answer(if i cant use getline
or similar) since reallocating memory for every single int value seems redundant to me.
Edit Since there has been some question about the code i provided and how to best use it in order to fulfill the OP's question. The codesnippet was supposed to showcase how one might create his own scanf-style function for unspecified number of integer inputs reading one full array at a time(in that case the free()
has to be omitted till one is done with the array(obviously).
edited Nov 19 at 19:08
answered Nov 19 at 18:37
ats
544
544
I do see a functional problem.scanf("%d", &c)
will read through an end-of-line like any other white-space. I'd expect OP's two lines of integers to end up in onearr
here.
– chux
Nov 19 at 19:02
The idea was to use my codesnippet as a standalone function & then just call it n times for the number of array's required. i admittedly omitted those details from my answer.
– ats
Nov 19 at 19:06
Trouble is we do not known
prior to reading the line.
– chux
Nov 19 at 19:08
Maybe i should've phrased that better. I did not mean n as in array length, but n as in number of array's read. Since the codesnippet can easily be packed inside a function and used as a sort of scanf-overload for array's
– ats
Nov 19 at 19:10
add a comment |
I do see a functional problem.scanf("%d", &c)
will read through an end-of-line like any other white-space. I'd expect OP's two lines of integers to end up in onearr
here.
– chux
Nov 19 at 19:02
The idea was to use my codesnippet as a standalone function & then just call it n times for the number of array's required. i admittedly omitted those details from my answer.
– ats
Nov 19 at 19:06
Trouble is we do not known
prior to reading the line.
– chux
Nov 19 at 19:08
Maybe i should've phrased that better. I did not mean n as in array length, but n as in number of array's read. Since the codesnippet can easily be packed inside a function and used as a sort of scanf-overload for array's
– ats
Nov 19 at 19:10
I do see a functional problem.
scanf("%d", &c)
will read through an end-of-line like any other white-space. I'd expect OP's two lines of integers to end up in one arr
here.– chux
Nov 19 at 19:02
I do see a functional problem.
scanf("%d", &c)
will read through an end-of-line like any other white-space. I'd expect OP's two lines of integers to end up in one arr
here.– chux
Nov 19 at 19:02
The idea was to use my codesnippet as a standalone function & then just call it n times for the number of array's required. i admittedly omitted those details from my answer.
– ats
Nov 19 at 19:06
The idea was to use my codesnippet as a standalone function & then just call it n times for the number of array's required. i admittedly omitted those details from my answer.
– ats
Nov 19 at 19:06
Trouble is we do not know
n
prior to reading the line.– chux
Nov 19 at 19:08
Trouble is we do not know
n
prior to reading the line.– chux
Nov 19 at 19:08
Maybe i should've phrased that better. I did not mean n as in array length, but n as in number of array's read. Since the codesnippet can easily be packed inside a function and used as a sort of scanf-overload for array's
– ats
Nov 19 at 19:10
Maybe i should've phrased that better. I did not mean n as in array length, but n as in number of array's read. Since the codesnippet can easily be packed inside a function and used as a sort of scanf-overload for array's
– ats
Nov 19 at 19:10
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379386%2fhow-read-two-lines-numbers-from-input-to-2-arrays-c-language-without-knowing-t%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you accept a number max for array length ?
– Ôrel
Nov 19 at 17:03
1
If you care about line endings, then you either need to read one character at a time with
fgetc
, or read a line at a time withfgets
.– user3386109
Nov 19 at 17:06
Read characters until a new line is reached, if one isn't reached, resize your buffer? You'll want to use dynamic memory allocation probably.
– jacob
Nov 19 at 17:07
2
A good function for reading a line at a time is
getline
, which was standardized in POSIX.1-2008. In comparision tofgets
, it can read a line of arbitrary length into a dynamically allocated buffer. If you don't have thegetline
function, there are various standalone implementations of it, such as the one at github.com/ivanrad/getline .– Ian Abbott
Nov 19 at 17:19
You could use a
while
loop and some arbitrary input as break condition & dynamically alloc your input array(i think).– ats
Nov 19 at 17:25