0

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.

  • 1
    Wow, sorry. Didn't even realise there was a website like that. I'll be sure to mention what is wrong next time. – danielwestfall May 02 '17 at 14:30
  • Tip: when having trouble with code that has I/O, post the input used, output seen and output expected. – chux - Reinstate Monica May 02 '17 at 16:53
  • @Toby This question is off-topic on Code Review, and will get closed there. Read the last sentence of the prose, and the Code Review help center. – Nic May 02 '17 at 18:49
  • @Toby Oh, whoops, yeah. I didn't look at the timestamps. My comment is (aside from that bit accusing you of not reading the question) currently correct, though; could you remove yours to prevent confusion? – Nic May 03 '17 at 14:01

4 Answers4

1
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[]) {
    int first, last;
    FILE *fp;

    if (argc < 3) { // Check for correct number of args
        // print an error message?
        return -1; 
    }

    first = atoi(argv[1]);
    last = atoi(argv[2]);    
    fp = fopen(argv[3], "w");

    if (fp != NULL) {
       for (int i = 0; i <= (first - last); i++) {
            fprintf(fp, "%d\n", i + first);           
        }
        fclose(fp);
    } else {
         //print an error message?
    }
    return 0;
}
  • Removed array, since it is not needed.
  • Changed the loop condition from < to <= because :

    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

  • Moved fclose() inside the if because: https://stackoverflow.com/a/32674233/2394967

What you could add now is error handling(checking if the file opened successfully and checking user input), but that may not be required here.

Community
  • 1
  • 1
sknt
  • 963
  • 6
  • 16
0
 if (fp == NULL) 

should be changed to

 if (fp != NULL) 
Pras
  • 4,047
  • 10
  • 20
  • Is that the only thing that is wrong? – danielwestfall May 02 '17 at 13:57
  • Additionally you should add some checks like - checking if user really 3 arguments on command line,if last > first, if last - first is less than 100 – Pras May 02 '17 at 14:01
  • This is going to sound really stupid, but say I didn't know how many numbers were between last and first until I enter the command, is there anyway to make it so fprintf, doesn't print any uninitialised values. – danielwestfall May 02 '17 at 14:05
0

you are missing a '}' before fclose and a ';' after fopen just to get things compiling

also the bounds of your write iteration are wrong. You should change

for (int i=0; i<last; i++)

to

for (int i=0; i<c; i++) {
vfiskewl
  • 70
  • 7
0

You should check the success of your opening before starting to read or write in the file. It will avoid a lot of times trying to understand what is not working. I am not saying it causes a problem in your code though. But might.

Try something like that :

file = fopen(path, mode);
if (file == NULL)
  fprintf(stderr, "Could not open file corresponding to this path : %s\n", path);
else {
  // code
}

Also, I would avoid iterating through an array without using the array size as loop stop condition. You could encounter segmentation faults.

Try something like that instead may be :

for (int i = 0; i < array.size()  1; i++) {
   fprintf(fp, "%d\n", array[i]);
}

In C :

for (int i = 0; i < sizeof(array)  1; i++) {
   fprintf(fp, "%d\n", array[i]);
}

More details on how to iterate an array in C here.

Community
  • 1
  • 1
Badda
  • 1,329
  • 2
  • 15
  • 40