Help Reading Data from Specific Filenames

This is a discussion on Help Reading Data from Specific Filenames within the C Programming forums, part of the General Programming Boards category; Hi all, I have been assigned to do a simple program for my lab group, but I feel like a ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    3

    Help Reading Data from Specific Filenames

    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!

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    the syntax for fscanf is as follows.

    Code:
     int fscanf(FILE *stream, const char *format,...);
    When no one helps you out. Call google();

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    3
    Ok, I tried adding:
    Code:
    FILE *tmp;
    char d_name[20];
    .....
    if( strstr(ep->d_name,"sphere") != NULL)
                   {
                       puts (ep->d_name);
                       tmp = fopen(d_name, "r");
                       while(fscanf(tmp, "%lf %lf", &Rows, &Cols) != EOF);
                       {
                       printf("%lf %lf\n", Rows, Cols);
                       }
                   }
    I think fscanf would be in the right form? But I still get a Segmentation Fault. I'm a little confused with d_name and the "ep->d_name" portion.
    Help please, thanks...

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > tmp = fopen(d_name, "r");
    Try opening ep->d_name instead.

    Also, this ; is very bad - it makes the loop an expensive no-operation
    while(fscanf(tmp, "%lf %lf", &Rows, &Cols) != EOF);

    Also, try checking things like fopen() for errors.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    3
    YES you are right...thanks Salem! It works, I'll post if I encounter anymore problems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble Reading Data from Files
    By CConfusion in forum C Programming
    Replies: 11
    Last Post: 04-06-2006, 07:12 PM
  2. Reading encrypted data from file
    By cctoombs in forum C Programming
    Replies: 2
    Last Post: 02-13-2005, 01:59 AM
  3. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  4. Binary data reading
    By jdinger in forum C++ Programming
    Replies: 3
    Last Post: 03-07-2002, 05:56 PM
  5. Help reading data file...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-25-2002, 11:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21