Using the FILE struct? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Using the FILE struct?

Mr_SmithMr_Smith Posts: 33Member
Hi
Im writing a program which needs to be able to create a FILE struct on its own to work with the standard fread etc commands, does anyone know the format for the FILE struct on windows (MS Vis studio6)

From the header
truct _iobuf {
char *_ptr;
int _cnt;
char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
char *_tmpfname;
};
typedef struct _iobuf FILE;

im not to sure whats what, looking at other files loading brings me to think

ptr is a pointer to the current data
and base is the filename

which all seems to work except i cant get fseek to work, does anyone know what the other variables are for like a position or offset ?

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2003-8-23 11:25:28[/red][/b][hr]
    : Hi
    : Im writing a program which needs to be able to create a FILE struct on its own to work with the standard fread etc commands, does anyone know the format for the FILE struct on windows (MS Vis studio6)
    :
    : From the header
    : truct _iobuf {
    : char *_ptr;
    : int _cnt;
    : char *_base;
    : int _flag;
    : int _file;
    : int _charbuf;
    : int _bufsiz;
    : char *_tmpfname;
    : };
    : typedef struct _iobuf FILE;
    :
    : im not to sure whats what, looking at other files loading brings me to think
    :
    : ptr is a pointer to the current data
    : and base is the filename
    :
    : which all seems to work except i cant get fseek to work, does anyone know what the other variables are for like a position or offset ?
    :

    [blue]It's not necessary, nor is it advisable, to monkey abound with the members of the FILE structure. All you, the programmer, have to do is pass the pointer returned by fopen() around to all the other functions such as fseek, fread, fwrite, fgets, fprintf, fclose, etc.

    You can find a description of all these functions and their parameters is nearly every textbook written about C language and on many on-line tutorials. you will also find them at www.msdn.microsoft.com.

    If you can't get fseek() to work, you need to post some code so that we can see what you are doing.

    [/blue]



  • Mr_SmithMr_Smith Posts: 33Member
    [b][red]This message was edited by Mr_Smith at 2003-8-23 12:15:52[/red][/b][hr]
    [b][red]This message was edited by Mr_Smith at 2003-8-23 11:30:19[/red][/b][hr]
    : [b][red]This message was edited by stober at 2003-8-23 11:25:28[/red][/b][hr]
    : : Hi
    : : Im writing a program which needs to be able to create a FILE struct on its own to work with the standard fread etc commands, does anyone know the format for the FILE struct on windows (MS Vis studio6)
    : :
    : : From the header
    : : truct _iobuf {
    : : char *_ptr;
    : : int _cnt;
    : : char *_base;
    : : int _flag;
    : : int _file;
    : : int _charbuf;
    : : int _bufsiz;
    : : char *_tmpfname;
    : : };
    : : typedef struct _iobuf FILE;
    : :
    : : im not to sure whats what, looking at other files loading brings me to think
    : :
    : : ptr is a pointer to the current data
    : : and base is the filename
    : :
    : : which all seems to work except i cant get fseek to work, does anyone know what the other variables are for like a position or offset ?
    : :
    :
    : [blue]It's not necessary, nor is it advisable, to monkey abound with the members of the FILE structure. All you, the programmer, have to do is pass the pointer returned by fopen() around to all the other functions such as fseek, fread, fwrite, fgets, fprintf, fclose, etc.
    :
    : You can find a description of all these functions and their parameters is nearly every textbook written about C language and on many on-line tutorials. you will also find them at www.msdn.microsoft.com.
    :
    : If you can't get fseek() to work, you need to post some code so that we can see what you are doing.
    :
    : [/blue]
    :


    What im doing is loading a file from a custom archive, i have

    char *Data
    int Length

    i want this to be put into the FILE form

    FILE *FileOut;
    FileOut->_base= myFile->Data;
    FileOut->_ptr = myFile->Data;
    FileOut->_cnt = myFile->length;

    as i havent got the option of modifing the code that reads it only the file pointer i send to it.


    Also MSDN doesnt seem to have ANY info on the FILE struct :(



  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2003-8-23 15:18:3[/red][/b][hr]
    [red]You can NOT do that![/red] call fopen() to set all those FILE objects and setbuf() if you want to set a local i/o buffer or make it unbuffered. The link below is for fopen() and from there you can navigate to the other functions.

    if myFile is also FILE and was opened with fopen(), then just simply set FileOut = myFile;

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_fopen.2c_._wfopen.asp


  • Mr_SmithMr_Smith Posts: 33Member
    : [b][red]This message was edited by stober at 2003-8-23 15:18:3[/red][/b][hr]
    : [red]You can NOT do that![/red] call fopen() to set all those FILE objects and setbuf() if you want to set a local i/o buffer or make it unbuffered. The link below is for fopen() and from there you can navigate to the other functions.
    :
    : if myFile is also FILE and was opened with fopen(), then just simply set FileOut = myFile;
    :
    : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_fopen.2c_._wfopen.asp
    :
    :
    :



    Thats the problem im NOT using fopen, i AM using fopen to open the archive but them im decompressing it and translating it back into its original form, so the file structure is long since lost but i NEED it back into a FILE form for as i dont have access to the source of the functions that use this data, why cant i create a FILE object its just a datastream, and i have the data in the memory?
  • stoberstober Posts: 9,765Member ✭✭✭
    :
    : Thats the problem im NOT using fopen, i AM using fopen to open the archive but them im decompressing it and translating it back into its original form, so the file structure is long since lost but i NEED it back into a FILE form for as i dont have access to the source of the functions that use this data, why cant i create a FILE object its just a datastream, and i have the data in the memory?
    :

    Where are you getting that FILE structure from ? Some other process? Some other computer? If either of these are true then it will be invalid in your program's address space anyway.

    One reason you cannot do that is because the FILE structure contains an operating system-allocated handle to the actual file and the only way to guarentee a valid handle is to get it from the operating system via fopen().

    The FILE pointer is NOT allocated using either malloc() or new, but is instead a pointer into an array of statically allocated FILE structures. You can verify this yourself by using your debugger to step into the fopen() function.
  • Mr_SmithMr_Smith Posts: 33Member
    : :
    : : Thats the problem im NOT using fopen, i AM using fopen to open the archive but them im decompressing it and translating it back into its original form, so the file structure is long since lost but i NEED it back into a FILE form for as i dont have access to the source of the functions that use this data, why cant i create a FILE object its just a datastream, and i have the data in the memory?
    : :
    :
    : Where are you getting that FILE structure from ? Some other process? Some other computer? If either of these are true then it will be invalid in your program's address space anyway.
    :
    : One reason you cannot do that is because the FILE structure contains an operating system-allocated handle to the actual file and the only way to guarentee a valid handle is to get it from the operating system via fopen().
    :
    : The FILE pointer is NOT allocated using either malloc() or new, but is instead a pointer into an array of statically allocated FILE structures. You can verify this yourself by using your debugger to step into the fopen() function.
    :


    Oh well as far as im concerned that code works for EVERYTHING i need except fseek it works for

    fread and fgets

    with NO problems and no invalid data being returned all i really want is a hacky solution and i know that almost works, if as u say the os uses this array then is it really only used for moving the pointer in the array ??

    and i know im only gunna get a hacking solution not a perfect 1 but it still basically works
  • tom_swtom_sw Posts: 828Member
    : : :
    : : : Thats the problem im NOT using fopen, i AM using fopen to open the archive but them im decompressing it and translating it back into its original form, so the file structure is long since lost but i NEED it back into a FILE form for as i dont have access to the source of the functions that use this data, why cant i create a FILE object its just a datastream, and i have the data in the memory?
    : : :
    : :
    : : Where are you getting that FILE structure from ? Some other process? Some other computer? If either of these are true then it will be invalid in your program's address space anyway.
    : :
    : : One reason you cannot do that is because the FILE structure contains an operating system-allocated handle to the actual file and the only way to guarentee a valid handle is to get it from the operating system via fopen().
    : :
    : : The FILE pointer is NOT allocated using either malloc() or new, but is instead a pointer into an array of statically allocated FILE structures. You can verify this yourself by using your debugger to step into the fopen() function.
    : :
    :
    :
    : Oh well as far as im concerned that code works for EVERYTHING i need except fseek it works for
    :
    : fread and fgets
    :
    : with NO problems and no invalid data being returned all i really want is a hacky solution and i know that almost works, if as u say the os uses this array then is it really only used for moving the pointer in the array ??
    :
    : and i know im only gunna get a hacking solution not a perfect 1 but it still basically works
    :
    Kinda sounds like you are getting similarly named constructs confused. The file structure of your compressed file shouldn't have anything to do with the FILE structure used to open and read from the file. The FILE structure contains information allowing the OS (and your program) to get at the data contained in the file, but doesn't specify what the data in the file means (i.e. how the data in the file is structured). That is the job of your program. The FILE handle doesn't seem to be the place to be doing that.
    If the file was originally compressed using an OS utility, then use that utility to read the data and copy it to an uncompressed file. If the file is a compressed data file (like a WinZip file), then you need to understand the algorithm that originally compressed the file, and do the reverse to restore it.
    I'm guessing from your comments that you have some data stored in a legacy format that is no longer supported and you need to get the data out and into a modern system/format.
  • Mr_SmithMr_Smith Posts: 33Member
    [b][red]This message was edited by Mr_Smith at 2003-8-29 2:25:41[/red][/b][hr]
    [b][red]This message was edited by Mr_Smith at 2003-8-29 2:24:44[/red][/b][hr]
    Yes, the problem is i have uncompressed it thats why its no longer in a FILE struct, it was in a form of LZS, i was just hoping to be able to make it so i could access it like ANY other file spoose i could write it out then read it back in ergghhhh

    neway for the moment i have an f_FILE wrapper that takes care of it by storing my file type and the normal 1
    then commands like f_open f_read etc :( all with if statements errgghhh



    i dont believe im confused at all, as i said earlier i was looking for a bodge to make it nicer to read the code rather than loads of nasty wrappers and rewritting lots of code that takes a FILE struct



    Also whats the point of uncompressing it with a utility when the whole idea is for a compressed data structure to distribute with the app

  • veliusvelius Posts: 456Member
    You sure are confused. You can't even use proper English. Instead your lazy when typing. Its no wonder you're looking for a quick hack. Anyways the FILE structure is used only to describe in an implementation specific way that the file is written on disk using the current file system. It is not meant to be used a file descriptor. You have your own file format, and it is compressed data. You need the algorithm to uncompress it in memory and either store it on disk temporarily or use a memory buffer. I'd suspect that the later may not be a solution due to you can not know how much memory a target machine will have if the file is large when uncompressed. Even if you set a minimum system requirement on memory it may not be enough, though. The FILE structure is meant only to be used to open a file and get a valid handle so you can (LIKE stober SAID) use the other functions such as fgets, fseek, fwrite, fread, etc. You need to define your very own structure that describes your file format and use binary reading and writing operations.



    [hr]
    When it came down to it I dropped everything and ran.
    -Ramza Final Fantasy Tactics

  • Mr_SmithMr_Smith Posts: 33Member
    [b][red]This message was edited by Mr_Smith at 2003-8-29 4:36:53[/red][/b][hr]
    huh how many times do i have to write this ive uncompressed it, thats why its now in my own file structure i was HOPEING to be able to bodge it into the file struct so as to avoid rewriting a load of loading and morphing routines i already have but as libraries obviously no one is capable of reading here so il do what i said........... use a wrapper....


    AND i would use proper english if i hadnt had to repeat myself this many times............................
    but then it was only about 5 words which wernt...



Sign In or Register to comment.