Good evening,
I'm having trouble with an assignement.
Basically we're required to code a program which will calculate the prime factors of a given stdin
. The data may only enter the program through its stdin
, be it throuh an echo
or a < file.txt
. The stream of data will never be greater than 80 characters (they can be numbers or not).
The functions I use in the program are read()
, strotol()
and strtok()
, and the "irrelevant" code flows as follows:
- Uses
malloc
to allocate 80 initial bytes of memory. - Stores in an
int
, throughread()
the number of characters read (and I believe, the last\0
). - reallocates memory with
realloc()
so as to save as much memory (I know in this case it's trivial, but oh well...).
Now comes the tricky bit:
- Since the data has to be separated with spaces, the max amount of items to check are at most:
(n/2)+1
, wheren
is the number of characters read in the upper point nº 2. - Creates an array of
long
with max size the number obtained in point nº 1. - Fills
numbers[0]
with the result of:strtol(strtok(line, delim), &end, 10)
. Adds
1
to thecounter
and enters awhile
loop:while((numbers[counter] = strtol(strtok(NULL, delim), &end, 10)) != NULL) { if(!*end) { // Check whether it's 0, 1, negative, prime or extract its factors } else { fprintf(stderr, "\"%s\" is not a non-negative integer", end) } counter++; }
Now, here are some inputs and their outputs:
Input: echo 1 2 3 4 5 6 7 8 9 10 | ./factors
Output:
1
2
3
2 2
5
2 3
7
2 2 2
3 3
2 5
Segmentation Fault (core dumped).
Input ./factors < integers.txt
Where integers contains a COLUMN of integers.
Output:
All the integers are factorised just fine and at the end it prints a:
Segmentation Fault (core dumped).
Input: echo abc 12 13 14 15 | ./factors
Output:
"abc" is not a non-negative integer
13
2 7
3 5
Segmentation Fault (core dumped).
Input: echo -1 -2 -3 -4 -5 | ./factors
Output:
"-1" is not a non-negative integer
"-2" is not a non-negative integer
"-3" is not a non-negative integer
"-4" is not a non-negative integer
"-5" is not a non-negative integer
Input: echo abc abc abc | ./factors
Output:
"abc" is not a non-negative integer
(And does not continue checking).
Input: echo 3 4 0 6 7 | ./factors
Output:
3
2 2
(And does not continue checking).
So as far as I can see, it fails when it encounters a 0
, more than one instance of a non-integer
or basically at the end of a healthy integer
-based stream of data.
Any idea how I could tackle this while and why is it failing so apparently randomly?
I should let you know I'm new to C...
Thank you very much in advanced.
====================================================
EDIT1: As per request, here are the code fragments which generate numbers[]
, and read from stdin
:
char *line;
char *end;
char *delim = " \n";
int charsread, counter;
line = malloc(80);
charsread = read(0, line, 81);
if (charsread == 0) {
return EX_OK;
}
realloc(line, charsread);
maxelem = (charsread / 2) + 1;
long numbers[maxelem];
numbers[0] = strtol(strtok(line, delim), &end, 10);
if (!*end) {
zeroone(numbers[0]);
}
else {
fprintf(stderr, "\"%s\" is not a non-negative integer\n", end);
}
counter = 1;
while [...]