I had a look now at fgets/fgetc, it is not keeping track of position with a static variable.

It is hidden in FILE struct:

struct __stdio_file {
  unsigned char *bufpos;   /* the next byte to write to or read from */
  unsigned char *bufread;  /* the end of data returned by last read() */
  unsigned char *bufwrite; /* highest address writable by macro */
  unsigned char *bufstart; /* the start of the buffer */
  unsigned char *bufend;   /* the end of the buffer; ie the byte after the last
                              malloc()ed byte */

  int fd; /* the file descriptor associated with the stream */
  int mode;

  char unbuf[8];       /* The buffer for 'unbuffered' streams */

  struct __stdio_file * next;
};


(example from dev86/bcc)

Nils

-------- Forwarded Message --------

Subject:        

Re: [Freedos-user] Preloading text files in C

Date:   

Thu, 10 Jan 2019 06:34:01 +0100

From:   

stecdose <[email protected]>

To:     

[email protected]


Hi Ken and TK Chia,


fmemopen looks very good, but sadly not available in Watcom. This is not a big problem, rewriting the code to use buffers is not that much work.

I am using a own putc/puts-function-pair, which directly writes to video memory. This lets me easily change it to another data source. I "saved" fmemopen to my list of things to remember for future projects.

I will implement it similar to this:


int number;

char linebuf[81];

number = preload("filename.txt");

...

while(get_line_from_buffer(number, &linebuf, 81) != NULL) {

    do_something();

}


I'll havea look at fgets how this one is storing the position in file and do it this way. I assume it is a static or global variable keeping track of position. I had a look at different fmemopen implementations and there's a lot of stuff I do not totally understand...


Thank you very much to both of you!


Nils



On 1/9/19 3:35 PM, TK Chia wrote:

Hello Nils, hello Ken,


ptr_array[0] = malloc(size_of_textfile);

copy_to(file0, ptr_array[0];

ptr_array[1] = malloc(size_of_textfile);

copy_to(file1, ptr_array[1];

...

Can I still use fgets() on these pointers once the file has been loaded

there?

No, fgets wants a FILE *, which is a pointer to a stream, not a char *.

If you are not able to rewrite the program to process the buffer

directly, forgetting about fgets(), there is fmemopen() which returns a

FILE * given a string. But it may not be implemented in your C library.

https://pubs.opengroup.org/onlinepubs/9699919799/functions/fmemopen.html


I just found that the Newlib library as included in the gcc-ia16 toolchain does happen to implement fmemopen( ) (and several other POSIX functions as well).


However, Open Watcom's C library does not have this function.


I suppose, if a `FILE *' is not absolutely needed, then one can use strchr( ), strcspn( ), etc. to slice the input up into separate lines, without writing a lot of code.


Thank you!


_______________________________________________
Freedos-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freedos-user

Reply via email to