Вы находитесь на странице: 1из 24

File System Structure

The Unix file system is a hierarchical structure that allows users to store information
by name. At the top of the hierarchy is the root directory, which always has the
name /. A typical Unix file system might look like:

The location of a file in the file system is called its path. Since the root directory is at
the top of the hierarchy, all paths start from /. Using the sample file system illustrated
above, let us determine the path for quiz1.txt:

All paths start from the root directory, so the path begins with /
We need to go to the home subdirectory, and the path becomes /home

In the home directory, we go into the ian subdirectory. The path is now
/home/ian

In the ian directory, we go into the cpsc124 subdirectory. The path is now
/home/ian/cpsc124

The file quiz1.txt is in the cpsc124 directory. Appending the filename to the
path, the path becomes /home/ian/cpsc124/quiz1.txt

Files and Directories


There are 3 kinds of files: ordinary files (files), directory files (directories), and special
files.

Files: Files are containers for data. This data can be anything, including the
text of a report, an image (picture) of a house, an executable program like a
word processor, or any arbitrary data. Files are created by users or programs in
order to save data for future use. For example, a user might save the report she
wrote using a text editor into a text file, or she might save an image from a
drawing program into an image file.

Directories: Each directory contains a number of files. A directory can contain


other directories, be contained in another directory, or both. A directory that
contains a file is called that file's parent directory. Similarly, if directory A
contains directory B, then directory A is directory B's parent directory. A
directory that is contained in another directory is called a subdirectory.

Special files: A special file is much like an ordinary file, and shares the same
basic interface. However, special files are not stored in the file system because
they represent input/output devices. The I/O device provides the data directly.
You will not responsible for knowing how special files work or even how to use
them. Simply be aware that they are present.

Every file has an associated data structure that contains important information about
that file. This data structure is known as an i-node. An i-node has information about
the length of the file, the time the file was most recently modified or accessed, the time
the i-node itself was most recently modified, the owner of the file, the group
identifications and access permissions, the number of links to the file (we will discuss
these shortly), and pointers to the location on the disk where the data contained in
the file is stored. A filename is used to refer to an i-node. A directory is a file that
contains a list of names, and for each name there is a pointer to the i-node for the file
or directory. The following picture offers a graphical interpretation of files, their inodes, and the blocks that they occupy on disk:

OPEN SYSTEMCALL:
Opening or creating a file can be done using the system open system call.

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
Function Definition
int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);
Field
Description
const char The relative or absolute path to the file
*path
that is to be opened.
whether it should be read only,
int oflags read/write, whether it should be cleared
when opened, etc
Modes determine the permissions of the
mode_t
file if it is created.
mode

return
value

1 for ok,-1 on error

Code Snippet
Example using the open system call:

#include <unistd.h>
#include <fcntl.h>
int main()
{
size_t filedesc = open("testfile.txt", O_WRONLY |
O_APPEND);
if(filedesc < 0)
return 1;
if(write(filedesc,"This will be output to testfile.txt\n",
36) != 36)
{
write(2,"There was an error writing to
testfile.txt\n",43);
return 1;
}

return 0;

Available Values for oflag


Value

Meaning

O_RDONLY Open the file so that it is read only.


O_WRONLY Open the file so that it is write only.

O_RDWR
Open the file so that it can be read from and written to.
O_APPEND Append new information to the end of the file.
O_TRUNC Initially clear all data from the file.
If the file does not exist, create it. If the O_CREAT option is used, then you
O_CREAT
must include the third parameter.
Combined with the O_CREAT option, it ensures that the caller must create
O_EXCL
the file. If the file already exists, the call will fail.
Available Values for mode
Value
S_IRUSR
S_IWUSR
S_IXUSR
S_IRGRP
S_IWGRP
S_IXGRP
S_IROTH
S_IWOTH
S_IXOTH

Meaning
Set
Set
Set
Set
Set
Set
Set
Set
Set

read rights for the owner to true.


write rights for the owner to true.
execution rights for the owner to true.
read rights for the group to true.
write rights for the group to true.
execution rights for the group to true.
read rights for other users to true.
write rights for other users to true.
execution rights for other users to true.

READ:
read is a system call used to read data into a buffer.
#include <unistd.h>
Function Definition
size_t read(int fildes, void *buf, size_t nbytes);
Field
Description
The file descriptor of where to read the input. You can either use a file
int fildes descriptor obtained from the open system call, or you can use 0, 1, or 2, to
refer to standard input, standard output, or standard error, respectively.
const
A character array where the read content will be stored.
void *buf
size_t
The number of bytes to read before truncating the data. If the data to be read
nbytes
is smaller than nbytes, all data is saved in the buffer.

return
value

Returns no of bytes on ok,-1 on error

Code Snippet
#include <unistd.h>
int main()
{
char data[128];
if(read(0, data, 128) < 0)
write(2, "An error occurred in the read.\n", 31);

exit(0);

WRITE:
write is a system call that is used to write data out of a buffer.
Required Include Files
#include <unistd.h>
Function Definition
size_t write(int fildes, const void *buf, size_t nbytes);
Field
Description
The file descriptor of where to write the output. You can either use a file
int fildes descriptor obtained from the open system call, or you can use 0, 1, or 2, to
refer to standard input, standard output, or standard error, respectively.
const
A null terminated character string of the content to write.
void *buf
size_t
The number of bytes to write. If smaller than the provided buffer, the output
nbytes
is truncated.
return
Returns the number of bytes that were written. If value is negative, then the
value
system call returned an error.
Code Snippet
Example using standard file descriptors:

#include <unistd.h>
int main(void)
{
if (write(1, "This will be output to standard out\n", 36) != 36) {
write(2, "There was an error writing to standard out\n", 44);
return -1;
}

return 0;

Example using a file descriptor:


#include <unistd.h>
#include <fcntl.h>
int main(void)
{
int filedesc = open("testfile.txt", O_WRONLY | O_APPEND);
if (filedesc < 0) {
return -1;
}
if (write(filedesc, "This will be output to testfile.txt\n", 36) != 36) {
write(2, "There was an error writing to testfile.txt\n", 43);
return -1;
}

return 0;

CLOSE:

close is a system call that is used to close an open file descriptor.


Required Include Files
#include <unistd.h>
Function Definition

int close(int fildes);


Field

Description

int fildes The file descriptor to be closed.


Retuns a 0 upon success, and a -1 upon failure. It is important to check the
return
return value, because some network errors are not returned until the file is
value
closed.
Code Snippet
Example closing an open file descriptor:
#include <stdlib.h>
#include <fcntl.h>
int main()
{
size_t filedesc = open("testfile.txt", O_WRONLY | O_CREAT);
if(filedesc < 0)
return 1;
if(close(filedesc) < 0)
return 1;

return 0;

LSEEK:
lseek is a system call that is used to change the location of the read/write pointer of a
file descriptor. The location can be set either in absolute or relative terms.
Required Include Files
#include <unistd.h>
#include <sys/types.h>
Function Definition
off_t lseek(int fildes, off_t offset, int whence);
Field
Description
int fildes The file descriptor of the pointer that is going to be moved.

off_t
offset
int
whence
return
value

The offset of the pointer (measured in bytes).


The method in which the offset is to be interpreted (relative, absolute, etc.).
Legal values for this variable are provided at the end.
Returns the offset of the pointer (in bytes) from the beginning of the file. If
the return value is -1, then there was an error moving the pointer.

Code Snippet
The following is an example using the lseek system call.
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
int main()
{
int file=0;
if((file=open("testfile.txt",O_RDONLY)) < -1)
return 1;
char buffer[19];
if(read(file,buffer,19) != 19) return 1;
printf("%s\n",buffer);
if(lseek(file,10,SEEK_SET) < 0) return 1;
if(read(file,buffer,19) != 19) return 1;
printf("%s\n",buffer);

return 0;

The output of the preceding code is:


$ cat testfile.txt
This is a test file that will be used
to demonstrate the use of lseek.
$ ./testing
This is a test file
test file that will
Available Values for whence

Value

Meaning

SEEK_SET Offset is to be measured in absolute terms.


SEEK_CUR Offset is to be measured relative to the current location of the pointer.
SEEK_END Offset is to be measured relative to the end of the file
STAT:
stat is a system call that is used to determine information about a file based on its file
path.
Required Include Files
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
Function Definition
int stat(const char *path, struct stat *buf);
Field
Description
const char
*path
struct stat
*buf
return
value

The file descriptor of the file that is being inquired.


A structure where data about the file will be stored. A detailed look at all of
the fields in this structure can be found in the struct stat page.
Returns a negative value on failure.

Code Snippet
An example of code that uses the stat() system call is below.
#include
#include
#include
#include

<unistd.h>
<stdio.h>
<sys/stat.h>
<sys/types.h>

int main(int argc, char **argv)


{
if(argc != 2)
return 1;
struct stat fileStat;
if(stat(argv[1],&fileStat) < 0)

return 1;
printf("Information for %s\n",argv[1]);
printf("---------------------------\n");
printf("File Size: \t\t%d bytes\n",fileStat.st_size);
printf("Number of Links: \t%d\n",fileStat.st_nlink);
printf("File inode: \t\t%d\n",fileStat.st_ino);
printf("File Permissions: \t");
printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
printf("\n\n");
printf("The file %s a symbolic link\n", (S_ISLNK(fileStat.st_mode)) ? "is" : "is not");

return 0;

The output of this program is shown in the following set of commands:


Information on the current files in the directory:
$ ls -l
total 16
-rwxr-xr-x 1 stargazer stargazer 36 2008-05-06 20:50 testfile.sh
-rwxr-xr-x 1 stargazer stargazer 7780 2008-05-07 12:36 testProgram
-rw-r--r-- 1 stargazer stargazer 1229 2008-05-07 12:04 testProgram.c
Running the program with the file testfile.sh
$ ./testProgram testfile.sh
Information for testfile.sh
--------------------------File Size:
36 bytes
Number of Links:
1

File inode:
180055
File Permissions:
-rwxr-xr-x
The file is not a symbolic link
Running the program with the files testProgram.c
$ ./testProgram testProgram.c
Information for testProgram.c
--------------------------File Size:
1229 bytes
Number of Links:
1
File inode:
295487
File Permissions:
-rw-r--r-The file is not a symbolic link
FSTAT:
fstat is a system call that is used to determine information about a file based on its file
descriptor.
Required Include Files
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
Function Definition
int fstat(int fildes, struct stat *buf);
Field
int fildes
struct stat
*buf
return
value

Description

The file descriptor of the file that is being inquired.


A structure where data about the file will be stored. A detailed look at all of
the fields in this structure can be found in the struct stat page.
Returns a negative value on failure.

Code Snippet
An example of code that uses the fstat() system call is below.

#include
#include
#include
#include
#include

<unistd.h>
<fcntl.h>
<stdio.h>
<sys/stat.h>
<sys/types.h>

int main(int argc, char **argv)


{
if(argc != 2)
return 1;
int file=0;
if((file=open(argv[1],O_RDONLY)) < -1)
return 1;
struct stat fileStat;
if(fstat(file,&fileStat) < 0)
return 1;
printf("Information for %s\n",argv[1]);
printf("---------------------------\n");
printf("File Size: \t\t%d bytes\n",fileStat.st_size);
printf("Number of Links: \t%d\n",fileStat.st_nlink);
printf("File inode: \t\t%d\n",fileStat.st_ino);
printf("File Permissions: \t");
printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
printf("\n\n");
printf("The file %s a symbolic link\n\n", (S_ISLNK(fileStat.st_mode)) ? "is" : "is not");

return 0;

The output of this program is shown in the following set of commands:

Information on the current files in the directory:


$ ls -l
total 16
-rwxr-xr-x 1 stargazer stargazer 36 2008-05-06 20:50 testfile.sh
-rwxr-xr-x 1 stargazer stargazer 7780 2008-05-07 12:36 testProgram
-rw-r--r-- 1 stargazer stargazer 1229 2008-05-07 12:04 testProgram.c
Running the program with the file testfile.sh
$ ./testProgram testfile.sh
Information for testfile.sh
--------------------------File Size:
36 bytes
Number of Links:
1
File inode:
180055
File Permissions:
-rwxr-xr-x
The file is not a symbolic link
Running the program with the files testProgram.c
$ ./testProgram testProgram.c
Information for testProgram.c
--------------------------File Size:
1229 bytes
Number of Links:
1
File inode:
295487
File Permissions:
-rw-r--r-The file is not a symbolic link
DUP:
dup is a system call similar to dup2 in that it creates an alias for the provided file
descriptor. dup always uses the smallest available file descriptor. Thus, if we called
dup first thing in our program, then you could write to standard output by using file
descriptor 3 (dup uses 3 because 0, 1, and 2 are already taken by default). You can
determine the value of the new file descriptor by saving the return value from dup
Required Include Files

#include <unistd.h>
Function Definition
int dup(int fildes);
Field

Description

int fildes The file descriptor that you are attempting to create an alias for.
dup returns the value of the new file descriptor that it has created (which will
return
always be the smallest available file descriptor). A negative return value
value
means that an error occured.
Code Snippet
Using dup(), we can create an alias for standard output, as follows:
#include <unistd.h> /*Included for dup(2) and write(2)*/
#include <stdlib.h> /*Included for exit(3)*/
#define MESSAGE "Hey! Who redirected me?\r\n\0"
int main()
{
int newfd = dup(STDOUT_FILENO); /*Call dup for an aliased fd*/
char buff[] = MESSAGE;
if (newfd < 0) { /*Negative file descriptors are errors*/
exit(EXIT_FAILURE);
}
else if (write(newfd, buff, sizeof(buff)) < 0) { /*See: man 2 write*/
exit(EXIT_FAILURE);
}

return EXIT_SUCCESS;

You can also use dup() to redirect standard output by taking advantage of the fact that
it always uses the smallest available file descriptor:
#include
#include
#include
#include

<unistd.h>
<fcntl.h>
<sys/types.h>
<sys/stat.h>

#include <stdlib.h>
#define OUTPATH "output"
#define MESSAGE "Behold, Standard Out is now a file!"
int main()
{
/*First, we open a file for writing only"*/
int outputfd = -1;
outputfd = open(OUTPATH, O_WRONLY | O_CREAT | O_TRUNC,
S_IRWXU | S_IRGRP | S_IROTH);
/*If we have an error, we exit
* N.B. file descriptors less than one are invalid*/
if (outputfd < 0) {
perror("open(2) file: " OUTPATH);
exit(EXIT_FAILURE);
}
/*Next, we close Standard Out
The lowest file descriptor will now
be STDOUT_FILENO*/
if (close(STDOUT_FILENO) < 0) {
perror("close(2) file: STDOUT_FILENO");
close(outputfd);
exit(EXIT_FAILURE);
}
/*Afterwards, we duplicate outputfd onto STDOUT_FILENO,
exiting if the descriptor isn't equal to STDOUT_FILENO*/
if (dup(outputfd) != STDOUT_FILENO) {
perror("dup(2)");
close(outputfd); /*N.B. Remember to close your files!*/
exit(EXIT_FAILURE);
}
close(outputfd); /*If everything succeeds, we may close the original file*/
puts(MESSAGE); /*and then write our message*/

return EXIT_SUCCESS;

DUP2:
dup2 is a system call similar to dup in that it duplicates one file descriptor, making
them aliases, and then deleting the old file descriptor. This becomes very useful when
attempting to redirect output, as it automatically takes care of closing the old file
descriptor, performing the redirection in one elegant command. For example, if you
wanted to redirect standard output to a file, then you would simply call dup2,
providing the open file descriptor for the file as the first command and 1 (standard
output) as the second command.
Required Include Files
#include <unistd.h>
Function Definition
int dup2(int fildes, int fildes2);
Field

Description

int fildes The source file descriptor. This remains open after the call to dup2.
The destination file descriptor. This file descriptor will point to the same file
int fildes2
as filedes after this call returns.
return
dup2 returns the value of the second parameter (fildes2) upon success. A
value
negative return value means that an error occured.
Code Snippet
Using dup2(), we can redirect standard output to a file, as follows:
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
using namespace std;
int main()
{
//First, we're going to open a file
int file = open("myfile.txt", O_APPEND | O_WRONLY);
if(file < 0) return 1;
//Now we redirect standard output to the file using dup2
if(dup2(file,1) < 0) return 1;

//Now standard out has been redirected, we can write to


// the file
cout << "This will print in myfile.txt" << endl;
return 0;
}//end of function main

File Handling in C with Examples (fopen, fread, fwrite, fseek)


by Himanshu Arora on July 9, 2012
As with any OS, file handling is a core concept in Linux. Any system programmer
would learn it as one of his/her initial programming assignments. This aspect of
programming involves system files.

Through file handling, one can perform operations like create, modify, delete etc on
system files. Here in this article I try to bring in the very basic of file handling. Hope
this article will clear the top layer of this multilayer aspect.
File handling functions
In this article, we will cover the following functions that are popularly used in file
handling :
fopen()
FILE *fopen(const char *path, const char *mode);
The fopen() function is used to open a file and associates an I/O stream with it. This
function takes two arguments. The first argument is a pointer to a string containing
name of the file to be opened while the second argument is the mode in which the file
is to be opened. The mode can be :

r : Open text file for reading. The stream is positioned at the beginning of the
file.
r+ : Open for reading and writing. The stream is positioned at the beginning of
the file.
w : Truncate file to zero length or create text file for writing. The stream is
positioned at the beginning of the file.

w+ : Open for reading and writing. The file is created if it does not exist,
otherwise it is truncated. The stream is positioned at the beginning of the file.

a : Open for appending (writing at end of file). The file is created if it does not
exist. The stream is positioned at the end of the file.

a+ : Open for reading and appending (writing at end of file). The file is created if
it does not exist. The initial file position for reading is at the beginning of the
file, but output is always appended to the end of the file.

The fopen() function returns a FILE stream pointer on success while it returns NULL in
case of a failure.
fread() and fwrite()
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
The functions fread/fwrite are used for reading/writing data from/to the file opened by
fopen function. These functions accept three arguments. The first argument is a
pointer to buffer used for reading/writing the data. The data read/written is in the
form of nmemb elements each size bytes long.
In case of success, fread/fwrite return the number of bytes actually read/written
from/to the stream opened by fopen function. In case of failure, a lesser number of
byes (then requested to read/write) is returned.
fseek()
int fseek(FILE *stream, long offset, int whence);
The fseek() function is used to set the file position indicator for the stream to a new
position. This function accepts three arguments. The first argument is the FILE stream
pointer returned by the fopen() function. The second argument offset tells the amount
of bytes to seek. The third argument whence tells from where the seek of offset
number of bytes is to be done. The available values for whence are SEEK_SET,
SEEK_CUR, or SEEK_END. These three values (in order) depict the start of the file,
the current position and the end of the file.
Upon success, this function returns 0, otherwise it returns -1.
fclose()
int fclose(FILE *fp);

The fclose() function first flushes the stream opened by fopen() and then closes the
underlying descriptor. Upon successful completion this function returns 0 else end of
file (eof) is returned. In case of failure, if the stream is accessed further then the
behavior remains undefined.
The code

#include<stdio.h>
#include<string.h>
#define SIZE 1
#define NUMELEM 5
int main(void)
{
FILE* fd = NULL;
char buff[100];
memset(buff,0,sizeof(buff));
fd = fopen("test.txt","rw+");
if(NULL == fd)
{
printf("\n fopen() Error!!!\n");
return 1;
}
printf("\n File opened successfully through fopen()\n");
if(SIZE*NUMELEM != fread(buff,SIZE,NUMELEM,fd))
{
printf("\n fread() failed\n");
return 1;
}
printf("\n Some bytes successfully read through fread()\n");
printf("\n The bytes read are [%s]\n",buff);
if(0 != fseek(fd,11,SEEK_CUR))
{
printf("\n fseek() failed\n");

return 1;

printf("\n fseek() successful\n");


if(SIZE*NUMELEM != fwrite(buff,SIZE,strlen(buff),fd))
{
printf("\n fwrite() failed\n");
return 1;
}
printf("\n fwrite() successful, data written to text file\n");
fclose(fd);
printf("\n File stream closed through fclose()\n");

return 0;

The code above assumes that you have a test file test.txt placed in the same location
from where this executable will be run.
Initially the content in file is :
$ cat test.txt
hello everybody
Now, run the code :
$ ./fileHandling
File opened successfully through fopen()
Some bytes successfully read through fread()
The bytes read are [hello]
fseek() successful
fwrite() successful, data written to text file
File stream closed through fclose()

Again check the contents of the file test.txt. As you see below, the content of the file
was modified.
$ cat test.txt
hello everybody
hello
Umask
#include
#include
#include
#include

<sys/types.h>
<sys/stat.h>
<fcntl.h>
<stdio.h>

int main(void)
{
umask(0);
if (creat("foo", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH |
S_IWOTH) < 0)
perror("foo");
umask(S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (creat("bar", S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH |
S_IWOTH) < 0)
perror("bar");
return 0;
}

Mkdir:
#include<stdio.h>
#include<sys/stat.h>
int main()
{
int i;
char dirname[50];
for(i=0;i<2;i++)

{
sprintf(dirname,"A%d",i);
if((mkdir(dirname,00777))==-1)
{

fprintf(stdout,"error in creating dir\n");


}

}
CHMOD:
main()
{
char mode[4]="0777";
char buf[100]="/home/hello.t";
int i;
i = atoi(mode);
if (chmod (buf,i) < 0)
printf("error in chmod");
}
DIR *opendir( const char *dirname );
struct dirent *readdir( DIR *dirp );
int readdir_r( DIR *dirp, struct dirent *entry,
struct dirent **result );
void rewinddir( DIR *dirp );
int closedir( DIR *dirp );
int chdir( const char *path );
char *getcwd( char *buf, size_t size );
int open( const char * path , int oflag , ... );
int creat( const char * path, mode_t mode );
int link( const char *existing, const char *new );
int mkdir( const char *path, mode_t mode );
int unlink( const char *path );
int rmdir( const char *path );
int rename( const char *old, const char *new );
int stat( const char *path, struct stat *buf );
int fstat( int fd, struct stat *buf );
int access( const char *path, int amode );
long pathconf(const char *path, int name);
long fpathconf(int fd, int name);

mode_t umask( mode_t cmask );


int mkfifo( const char *path, mode_t mode );
int chmod( const char *path, mode_t mode );
int fchmod( int fd, mode_t mode );
int chown( const char *path, uid_t owner, gid_t group );
int utime( const char *path, const struct utimbuf *times );
int ftruncate( int fd, off_t length );

// TBA
// TBA
// TBA

/*
* This program displays the names of all files in the current directory.
*/
#include <dirent.h>
#include <stdio.h>
int main(void)
{
DIR
*d;
struct dirent *dir;
d = opendir(".");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
printf("%s\n", dir->d_name);
}
}

closedir(d);

return(0);
}

Вам также может понравиться