Hi all,
I have been assigned to do a simple program for my lab group, but I feel like a 5th grader doing Calculus (maybe not THAT C illiterate) but I know some basics of C and received help from some people but I don't understand the solution.
Anyway, I need help reading data from files that have the word "sphere" in it. I was suggested to use a directory lister of all the files with the word "sphere" in it, which works fine (the code is posted).
Code:
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main (void)
{
DIR *dp;
struct dirent *ep;
dp = opendir ("./");
if (dp != NULL)
{
while (ep = readdir (dp))
if( strstr(ep->d_name,"sphere") != NULL)
puts (ep->d_name);
(void) closedir (dp);
}
else
perror ("Couldn't open the directory");
return 0;
}
However, looking at just the IF statement, I thought maybe I could do:
Code:
if (dp != NULL)
{
while (ep = readdir (dp))
if( strstr(ep->d_name,"sphere") != NULL)
while(fscanf(d_name, "%lf %lf", &Rows, &Cols) != EOF);
}
Adding that fscanf of d_name to read the resulting "sphere" files, but of course it doesn't work.
Also, the directory listing of the "sphere" files are out of order, is there a way to sort them? If those sphere files get stored in an array, it'd be easy but I'm lost with that too.
Many Thanks!