-2

(note: This question is for some schoolwork I'm doing. I don't need to know how to implement this, just need to know if it's possible and how can I learn more about it)

I'd like to use a user level buffer for a file using only a file descriptor and not using a FILE*. I know stdio.h has, at least, 1 fifo user level buffer for all *printf() and *scanf() functions.

My question:

I was asked to do a system that uses the write() linux function. That function is a system call. The objective here is to complete a small extra challenge I was given by the professor which is:

Use or make a user level buffer for the writting of those files.

Because I use the function write and I use file descriptors I cannot use any of the fprintf() functions because it is documented that mixing system calls (write and *fprintf) has an undefined result.

Is there an alternative way to use the same buffer as stdio or is there an equivalent buffer "offered" by linux's libraries to write to that file? (Note: I also need to know how to flush that buffer)

I don't have special problems making a fifo buffer, but If I can save some work, that'll be useful.

Note: My code is for linux. If there is portable code possible for this, please tell me about the portable code. If there isn't any portable code, then please give me linux's way.

Bart
  • 19,692
  • 7
  • 68
  • 77
brunoais
  • 6,258
  • 8
  • 39
  • 59
  • 1
    It's not clear what you're trying to do... – R.. GitHub STOP HELPING ICE Nov 15 '11 at 07:04
  • I want to use a buffer for a file. Because I use write (requirements for the job) I may not use *fprintf because it is undocumented. I'm asking for alternatives – brunoais Nov 15 '11 at 07:06
  • 1
    I don't understand the question. Every FILE* handle has (and almost is) a buffer, and is calling internally the `write` system call to flush it. You can (and should) do your own buffering when using the `write` system call (because calling it for every character is too costly). But what is your question? How to do buffering? – Basile Starynkevitch Nov 15 '11 at 07:06
  • If you want to write into a memory buffer (without any real output to files) using stdio functions, consider using `open_memstream` or `fmemopen` – Basile Starynkevitch Nov 15 '11 at 07:07
  • @Basile Starynkevitch because I'm using the write, I only use int and not FILE*. I'll see those 2, thx – brunoais Nov 15 '11 at 07:10
  • Use `dprintf` but your question is really not clear (perhaps because your english is not good enough). If you are French, feel free to send me a private email in French. – Basile Starynkevitch Nov 15 '11 at 07:16
  • I'll try to improve the question. – brunoais Nov 15 '11 at 07:28
  • Is that better? Now I need to know how to flush dprintf buffer – brunoais Nov 15 '11 at 07:52

1 Answers1

0

Of course. You can collect whatever you want in a buffer and write() this buffer.

Try this:

...
size_t cursize = 100;
char * buf = malloc(cursize);
char * cursor = buf;
char * end = buf + cursize;
int n = snprintf(cursor, end-cursor, "Hello! Here is the number %d.", 12);
// maybe do some checking of n here; ensure that it is below 100
cursor += n; // advance the write cursor
n = sprintf(cursor, end-cursor, " And this is data I would like to add.");
// dito
cursor += n;

// Now we have buffered enough data and can write out our stuff.
write(fd, buf, cursor-buf);
free(buf);

Doing so gives you the flexibility to extend your buffer as soon as needed, and doing other stuff.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • I wanted something more complex... Still, I should be able to develop some initial ideas with that. – brunoais Nov 15 '11 at 14:07