Hi
I want to know...
can we able to create array of files and how ?????????

Dani AI

Generated

When you say "array of files" you probably mean an array of file handles (pointers or descriptors), not an array that contains disk files. , an array of FILE* or file descriptors is the usual approach. That’s what demonstrated, and is right to remind you that files themselves live on the filesystem — your program only holds references to them.

Practical points to watch for:

  • Keep a parallel list of filenames and the corresponding FILE* (or store both in a small struct) so you can report errors and close the right handle.
  • Always check the result of fopen/open. If one open fails, close any already-opened handles before exiting.
  • Don’t try to open huge numbers of files at once; OS limits exist (use sysconf(_SC_OPEN_MAX) or getrlimit(RLIMIT_NOFILE) on POSIX to check). If you need to process many files, open/process/close them in batches.
  • If multiple threads or processes access the same file, use proper locking (OS-specific) to avoid corruption.

Example pattern (dynamic array + error handling):

const char *names[] = { "in1.txt", "in2.txt", "in3.txt" };
size_t n = sizeof names / sizeof *names;
FILE **fps = malloc(n * sizeof *fps);
if (!fps) { perror("malloc"); exit(1); }
for (size_t i = 0; i < n; ++i) {
    fps[i] = fopen(names[i], "r");
    if (!fps[i]) { perror(names[i]); while (i-- > 0) fclose(fps[i]); free(fps); exit(1); }
}
/* use files */
for (size_t i = 0; i < n; ++i) fclose(fps[i]);
free(fps);

If your set of files is fixed and small, a static array is fine; for variable counts use dynamic allocation or a container. Handle errors and closes carefully — that’s the common pitfall.

Recommended Answers

All 3 Replies

you can try it by yourself. :)

You could certainly have an array of file HANDLES or the like...

FILE* myFiles[10];

for (int i = 0; i < 10; i++)
    myFiles[i] = fopen("Chainsaw.txt","r");

is that what you mean?

Hello,

An array of files makes no sense. Arrays are data structures; files are residents of a file system on a hard drive (or RAMdrive).

What are you really trying to do?

Christian

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.