I am trying to create a file and print a series of integers through the command line. Essentially what I want is for argv[1]
to be the first number printed and argv[2]
to be the last number printed and the series in between. argv[3]
will be the name of the file. My code is shown below so if anyone could point out any mistakes that would be great. Just assume for this example that the arguments received will always be four. First time using the file functions so I apologise for any really simple mistakes. Receiving an illegal array, pointer error.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int array[100];
int first, last, c;
first=atoi(argv[1]);
last=atoi(argv[2]);
c=last-first;
for (int a=0; a<c; a++) {
array[a]=first+a;
}
FILE *fp;
fp = fopen(argv[3], "w");
if (fp == NULL) {
for (int i=0; i<last; i++) {
fprintf(fp, "%d\n", array[i]);
}
}
fclose(fp);
return 0;
}
My input is something shown below.
./number.c 20 25 5.txt
My expected output when I type cat "file_name.txt" is shown below.
cat number.c
20
21
22
23
24
25
But the output I receive is
ASAN:DEADLY SIGNAL
runtime error-illegal array, pointer or other operation
Edit: Problem solved.