0

For the following code:

#include <stdio.h>

int main(int argc, char *argv[1])
{
    char *x = argv[1];
    char a[5];
    char b[5];
    FILE *fn = fopen(x, "r");
    FILE *fp;
    for(int i = 0; i < 5; i++)
    {
          sprintf(a, "%d.txt", i);
          fp = fopen(a, "w");
          fread(&b, 1, 1, fn);
          fwrite(&b, 1, 1, fp);
          fclose(fp);

    }

    return 0;
}

Does the fopen() function create 5 files namely, 0.txt, 1.txt, 2.txt, 3.txt, 4.txt? Why does it do so exactly? Also, how can I read a file into an array if I don't know the amount of data the file holds?

2
  • 1
    Why not to run the code by yourself, see what' happens (segm fault I suspect), and then ask?
    – obesman
    Jul 17, 2017 at 4:22
  • Yes, I have resolved it. It was a segfault. Thank you :)
    – Tejesh Atr
    Jul 17, 2017 at 5:48

0

You must log in to answer this question.

Browse other questions tagged .