[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [OT] Help with glibc memory streams



Decklin Foster wrote:
> 
> Eray Ozkural (exa) writes:
> 
> >    while (!feof(stream)) {
> 
> There's your problem. (well, one of them, at least.)
> 
> http://www.eskimo.com/~scs/C-faq/q12.2.html

Yea, not all of it. My example code is derived from GNU make's
readline function, and if you'd please compile and
run it you'll see that it doesn't print the string at all!!
The feof is redundant there, it's just there to make sure
I'm not getting mad. ;)

Problem restated: If I used a file stream instead of
a memory stream, the behavior would be completely different.
This code is quite important for a little hack I'm working
on so help would be appreciated.

I read the libc info documentation on I/O several times to
see if I was wrong but AFAICT the problem is not with my
code because according to the documentation this should work.

If it worked to my expectations, this wouldn't be the case:
[I expect it to behave the same for file and mem streams]

orion:libc$ ./filestream 
Read: testorion:libc$ 
orion:libc$ ./memstream 
read error!
orion:libc$ cat testfile
testorion:libc$ 

As you can see, it isn't able to print anything at all
when I use fmemopen :'(

I'm attaching the source code for both .c files. The
filestream.c is derived from memstream.c, using the
file "testfile" instead of the memory stream.

Thanks,

-- 
Eray (exa) Ozkural
Comp. Sci. Dept., Bilkent University, Ankara
e-mail: erayo@cs.bilkent.edu.tr
www: http://www.cs.bilkent.edu.tr/~erayo
#include <stdio.h>

void main()
{
   char *str = "test";
   char line[80];
   FILE *stream;
   
   stream = fmemopen(str, strlen(str), "r");
   while (!feof(stream)) {
      if (!fgets(line, 80, stream)) {
      	printf("read error!\n");
        exit(-1);
      }
      printf("Read: %s", line);
   }
   fclose(stream);
}
#include <stdio.h>

void main()
{
   char line[80];
   FILE *stream;
   
   stream = fopen("testfile", "r");
   while (!feof(stream)) {
      if (!fgets(line, 80, stream)) {
      	printf("read error!\n");
        exit(-1);
      }
      printf("Read: %s", line);
   }
   fclose(stream);
}
test

Reply to: