This code is supposed to run through a loop and write data to a file descriptor.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void format(int n, char *d, FILE *f);
int main()
{
char *d="written";
FILE *f;
int i, n=0;
for(i=0; i<=5; i++)
{
format(n, d, f);
fprintf(f, "stack #1, 15");
fclose(f);
}
return 0;
}
void format(int n, char *d, FILE *f)
{
char fm[16];
n++;
snprintf(fm, 16, "%s.%d.txt", d, n);
f = fopen(fm, "w");
}
When I run this program I get a "segmentation fault" and I'm not sure why. I'm guessing it has to do with format().
How can I use format() to correctly write data to a fd?
Thanks!