herzl shemuelian
herzl shemuelian

Reputation: 3498

Read and write via FILE stream to raw memory

I write to c raw buffer via FILE stream that I open file like below:

 char fileBuf[1024];
 FILE *fp = fopen("nul", "wb");
 setbuf(fp, fileBuf);
 fprintf(fp, "my str");

then when I write to file I write to my buffer fileBuf

but I need some way to read from fileBuf via FILE stream some like below?

 char local[100];
 FILE *f = fopen("nul", "rb");
 setbuf(f, fileBuf);
 fgets(local,sizeof(local),f);

(I work on windows and there don't exist memfile)

Upvotes: 0

Views: 114

Answers (1)

Uriel
Uriel

Reputation: 16184

You use a variable named local (don't, by the way, because it is a reserved keyword) but sets the buffer to fileBuf:

FILE *f = fopen("nul", "rb");
setbuf(f, fileBuf);
fgets(fileBuf, sizeof(fileBuf), f);

Upvotes: 1

Related Questions