Code:
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int copyfile(char *fromfile, char *tofile)
{
FILE *ifp, *ofp;
int c;
if((ifp = fopen(fromfile, "r")) == NULL) return -1;
if((ofp = fopen(tofile, "w")) == NULL) { fclose(ifp); return -1; }
while((c = getc(ifp)) != EOF)
putc(c, ofp);
fclose(ifp);
fclose(ofp);
return 0;
}
int main()
{
static MAX = 1000;
DIR *pdir;
struct dirent *ent;
char *c_file;
char *c_file_hold[MAX];
pdir = opendir(".");
if (!pdir)
{
printf ("opendir() failure; terminating");
exit(1);
}
int i = 0; //counter
while((ent=readdir(pdir)))
{
//strncpy(c_file , ent->d_name, 10);
printf( "%s\n" , ent->d_name);
if(istrstr(ent->d_name, ".c") ? 1 : 0)
{
sprintf(c_file, "cc %s", ent->d_name); //THIS HERE IS GIVING ME A SEGMENTATION FAULT
system(c_file);
system("a.out");
}
}
closedir(pdir);
}
Anyone know hwat I am doing wrong here? How can i get this to compile ?