Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Howto: Open multiple files with different handles 3

Status
Not open for further replies.

thenewa2x

Programmer
Dec 10, 2002
349
US
I want to use an array to hold file pointers and another value (most likely the path or name to the file).

[tt]char handles[99][2];[/tt]

How would I be able to store a file pointer and a file path in:
[tt]
handles[handle_name][0] = FILE *fp;
handles[handle_name][1] = "/virtual/path/to/file.dat";
[/tt]

I just came up with a quick example and I don't even know if that works. [tt]handle_name[/tt] can be different "handles" for each two values.

[tt]
struct handles
{
char FILE *fp;
char path[256];
}

struct handles handle_name = { FILE *fp, file_path };
[/tt]

I don't know what to do, I just started learning C.

---------------------------------------
If you helped... thx.
 
Your declaration will be something like the following:

Code:
struct handles
{
        FILE    *fp;
        char    path[256];
};

struct handles handle_list[100];

Or
Code:
typedef struct
{
        FILE    *fp;
        char    path[256];
} HANDLE;

HANDLE handle_list[100];

To use it, try:
Code:
strcpy(handle_list[0].path,"/home/roger/myfile.txt");

handle_list[0].fp = fopen(handle_list[0].path,"r");
 
You are on the right way, but... Use, for example:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
  FILE* file;
  char  path[_MAX_PATH]; /* std macros from <stdlib.h> */
} FileInfo;
...
FileInfo handle[100]; /* define proper size with macros */
int handles = 0;

...
/* Add new 'handle' */
FILE* f = fopen(thepath,"r");
if (f)
{
  handle[handles].file = f;
  strcpy(handle[handles].path,thepath);
  handles++;
}
/* Now you may refer to i-th handle as: */
...handle[i].file /* C stream FILE* pointer */
...printf("Path[%d]:%s\n",i,handle[i].path);
Invent a function(s) to add and remove handles from handle array - and go on!...
Good luck!
 
Watch out for your OS limits on the number of files a process can open simultaneously.

For example, my linux box has
[tt]#define OPEN_MAX 256 /* # open files a process may have */[/tt]

--
 
wow this was incredibly helpful guys, THX!

---------------------------------------
If you helped... thx.
 
For these two functions I'm getting errors, can you please tell me what's wrong?

Code:
void vopen( char fh, char fm, char fn )
{
  if ( (handles[fh].fp = fopen(fn, fm)) == NULL )
    fprintf(stderr, "Couldn't open database file: %s", fn);

  strcpy(handles[fh].path, fn);
}
void vclose( char fh )
{
  fclose(handles[fh].fp);
  strcpy(handles[fh].fp,   "\0");
  strcpy(handles[fh].path, "\0");
}

And by the way, the handles are in characters, not integers: FILE, TEMP, OPEN, etc... (this C program to be used with perl).

---------------------------------------
If you helped... thx.
 
any help available? you guys did great for the first problem!

---------------------------------------
If you helped... thx.
 
> any help available? you guys did great for the first problem!
Gee, how long did you wait before bumping your thread to the top of the list again?
This is an international board, we don't all work to your clock, or your timescales.

Posting your actual error messages would be useful.

> void vopen( char fh, char fm, char fn )
Given your use of fopen, then these should be
void vopen( int fh, char *fm, char *fn )

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top