pilas
May 5th, 2011, 11:03 AM
Hello everyone, I was wondering if it is possible to know the name of a file name, after we open it with a FILE structure, for example:
#include<stdio.h>
int main(int argc, char *argv[])
{
FILE *fp;
fp = fopen ("file.txt", "w");
return 0;
}
after doing this, is it possible to know the name of the file we opened, using the fp variable?
what if we write in terminal ./program <"file.txt" , can we print the name of the file afterwards using some sort of code?? thanks!
I've tried to use printf("%s, ...); with all the fields in the FILE structure, but with no success... here are the fields I tested: (taken from http://www.opensource.apple.com/source/gcc/gcc-937.2/libio/libio.h)
struct _IO_FILE {
int _flags; /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags
/* The following pointers correspond to the C++ streambuf protocol. */
/* Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
char* _IO_read_ptr; /* Current read pointer */
char* _IO_read_end; /* End of get area. */
char* _IO_read_base; /* Start of putback+get area. */
char* _IO_write_base; /* Start of put area. */
char* _IO_write_ptr; /* Current put pointer. */
char* _IO_write_end; /* End of put area. */
char* _IO_buf_base; /* Start of reserve area. */
char* _IO_buf_end; /* End of reserve area. */
/* The following fields are used to support backing up and undo. */
char *_IO_save_base; /* Pointer to start of non-current get area. */
char *_IO_backup_base; /* Pointer to first valid character of backup area */
char *_IO_save_end; /* Pointer to end of non-current get area. */
struct _IO_marker *_markers;
struct _IO_FILE *_chain;
int _fileno;
int _blksize;
#ifdef _G_IO_IO_FILE_VERSION
_IO_off_t _old_offset;
#else
_IO_off_t _offset;
#endif
#define __HAVE_COLUMN /* temporary */
/* 1+column number of pbase(); 0 is unknown. */
unsigned short _cur_column;
char _unused;
char _shortbuf[1];
/* char* _save_gptr; char* _save_egptr; */
#ifdef _IO_LOCK_T
_IO_LOCK_T _lock;
#endif
#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
_IO_off64_t _offset;
int _unused2[16]; /* Make sure we don't get into trouble again. */
#endif
};
#include<stdio.h>
int main(int argc, char *argv[])
{
FILE *fp;
fp = fopen ("file.txt", "w");
return 0;
}
after doing this, is it possible to know the name of the file we opened, using the fp variable?
what if we write in terminal ./program <"file.txt" , can we print the name of the file afterwards using some sort of code?? thanks!
I've tried to use printf("%s, ...); with all the fields in the FILE structure, but with no success... here are the fields I tested: (taken from http://www.opensource.apple.com/source/gcc/gcc-937.2/libio/libio.h)
struct _IO_FILE {
int _flags; /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags
/* The following pointers correspond to the C++ streambuf protocol. */
/* Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
char* _IO_read_ptr; /* Current read pointer */
char* _IO_read_end; /* End of get area. */
char* _IO_read_base; /* Start of putback+get area. */
char* _IO_write_base; /* Start of put area. */
char* _IO_write_ptr; /* Current put pointer. */
char* _IO_write_end; /* End of put area. */
char* _IO_buf_base; /* Start of reserve area. */
char* _IO_buf_end; /* End of reserve area. */
/* The following fields are used to support backing up and undo. */
char *_IO_save_base; /* Pointer to start of non-current get area. */
char *_IO_backup_base; /* Pointer to first valid character of backup area */
char *_IO_save_end; /* Pointer to end of non-current get area. */
struct _IO_marker *_markers;
struct _IO_FILE *_chain;
int _fileno;
int _blksize;
#ifdef _G_IO_IO_FILE_VERSION
_IO_off_t _old_offset;
#else
_IO_off_t _offset;
#endif
#define __HAVE_COLUMN /* temporary */
/* 1+column number of pbase(); 0 is unknown. */
unsigned short _cur_column;
char _unused;
char _shortbuf[1];
/* char* _save_gptr; char* _save_egptr; */
#ifdef _IO_LOCK_T
_IO_LOCK_T _lock;
#endif
#if defined(_G_IO_IO_FILE_VERSION) && _G_IO_IO_FILE_VERSION == 0x20001
_IO_off64_t _offset;
int _unused2[16]; /* Make sure we don't get into trouble again. */
#endif
};