× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



On Tue, 28 Sep 2004, Hatzenbeler, Tim wrote:

I'm trying to use our C compiler for the 1st time...

I would like to write a program, to read all the 'filenames' in a single
directory in the IFS, and then open each file, I I would like to parse it,
and place the contents in a 'Table' I was wondering if any body has any code
snippets they sould share to get my feet wet...  I haven't touched 'C' code
in 10 years sine the Amiga.

Sorry to take so long to respond. Here is some code to get you started:

/* This program demonstrates using C to change directories
 * and list the files in a directory
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>

/* Define this to be the directory you want to change to */
#define NEW_DIR "/home"

int
main ()
{
  char dir[1024];
  DIR *directory;
  struct dirent *entries;

  /* First we demonstrate how to get the current directory
   * Notice that getcwd() returns a *char and dir also contains
   * the name of the directory
   */
  printf ("pwd = %s\n", getcwd (dir, sizeof (dir)));
  printf ("dir = %s\n", dir);

  /* Change to NEW_DIR and check for errors */
  if (chdir (NEW_DIR) != 0)
    {
      printf ("Eek! cannot chdir to %s\n", NEW_DIR);
      return (-1);
    }

  /* Here we erase dir since we are going to call getcwd() again
   * to prove that chdir() worked.
   */
/*bzero (dir, sizeof (dir));*/
  printf ("The files in %s are:\n", getcwd (dir, sizeof (dir)));

  /* Now open the directory we just changed to. */
  directory = opendir (NEW_DIR);

  /* Use a loop to print out all the files in the directory. */
  while ((entries = readdir (directory)) != NULL)
    {
      printf ("%s\n", entries->d_name);
    }

  /* Don't forget to close! */
  closedir (directory);

  /* Done */
  return (0);
}

James Rich

Vs lbh cynl n Zvpebfsg PQ  onpxjneqf, lbh pna urne fngnavp zrffntrf. Ohg
rira jbefr, vs lbh cynl vg sbejneq, vg vafgnyyf gurve fbsgjner!
        -- Fcbgvphf ba /.

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.