A QUICK C PROGRAM QUESTION

Kenji4861

Banned
Jan 28, 2001
2,821
0
0
I have this huge project I was working on and stuck on one thing.

How do I go about opening every file in a directory? I just want to open each file and read the material in the file, and to the next, and so on. Thanks in advance!
 

crystal

Platinum Member
Nov 5, 1999
2,424
0
76
If you know the directory, couldn't you just read all the files into an array? Loop through the array, and open it for read one by one.
 

XZeroII

Lifer
Jun 30, 2001
12,572
0
0
You would be a fool to ignore the boolean anti-binary least squared approach.
 

Kenji4861

Banned
Jan 28, 2001
2,821
0
0
How do I find out each file name in the directory?

Sorry, I think I'm missing something basic.
 

BCYL

Diamond Member
Jun 7, 2000
7,803
0
71
are u using linux?

do a 'man glob'... the 'glob' function is exactly what you want to use...
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
C in itself doesn't have any notion of accessing a directory. Depending on what OS you're using there's an API to find information about all the files in a directory.
 

BCYL

Diamond Member
Jun 7, 2000
7,803
0
71
Originally posted by: Kenji4861
I am programming in the UNIX environment.

'glob' should be available then.... do a man on it and you will see how to use it...

basically you specify the pattern to look for as "/your_directory/*' and it will give you all the filenames matching that pattern (which is all the files inside that directory)... then you can step through them one by one and do what you want with them...
 

michaelh20

Senior member
Sep 4, 2000
482
0
0
I think what you are looking for is something along these lines

This is sort of copied out of "Practical Unix Programming" (Robbins&Robbins)

You should do man search on opendir, DIR*, dirent , closedir, readdir,

#include <stdio.h> <stdlib.h> <string.h> dirent.h errno.h
DIR * dirp-- pointer to an opened dir
struct dirent *direntp -- pointer to a dir entry

dirp = opendir("name of dir");
while(direntp = readdir(dirp)) != NULL)
printf("%s\n", direntp->d_name); // there's probably a lot more av than just the name
closedir(dirp)
 

BCYL

Diamond Member
Jun 7, 2000
7,803
0
71
Originally posted by: Kenji4861
Can I call glob in C? It seems like glob is just a program to run in UNIX.

Yes you can call glob in C... it's just like any other function call... just include the *.h file specified at the top of the man page...